-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.cpp
52 lines (49 loc) · 1.05 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0);cin.tie(0)
long long ara[200005],ans[200005],vis[200005];
int main()
{
fastio;
long long n,m,i,j,u,v,w;
cin>>n>>m;
for(i=0;i<n;i++)
cin>>ara[i];
vector<pair<long long,long long> > adj[n+5];
for(i=0;i<m;i++)
{
cin>>u>>v>>w;
u--,v--;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
ans[0]=ara[0];
vis[0]=1;
queue<long long> q;
q.push(0);
while(!q.empty())
{
u=q.front();
q.pop();
for(i=0;i<adj[u].size();i++)
{
v=adj[u][i].first;
w=adj[u][i].second;
if(vis[v]==0)
{
vis[v]=1;
ans[v]=ans[u]+ara[v]+w;
q.push(v);
}
else if(ans[v]>ans[u]+ara[v]+w)
{
ans[v]=ans[u]+ara[v]+w;
q.push(v);
}
}
}
for(i=1;i<n;i++)
cout<<ans[i]<<" ";
cout<<"\n";
return 0;
}