-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11657.cpp
More file actions
49 lines (49 loc) · 1017 Bytes
/
11657.cpp
File metadata and controls
49 lines (49 loc) · 1017 Bytes
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
#include<iostream>
#include<vector>
using namespace std;
#define INF 987654321
long long dist[501];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<pair<int,pair<int,int>>>v;
int N,M,a,b,c;
cin>>N>>M;
for(int i=1;i<=N;i++)
dist[i]=INF;
dist[1]=0;
for(int i=0;i<M;i++)
{
cin>>a>>b>>c;
v.push_back({c,{a,b}});
}
bool ncycle=false;
for(int i=1;i<=N;i++)
{
for(int j=0;j<M;j++)
{
int x=v[j].second.first;
int y=v[j].second.second;
int cost=v[j].first;
if(dist[x]!=INF && dist[y]>dist[x]+cost){
dist[y]=dist[x]+cost;
if(i==N){
ncycle=true;
}
}
}
}
if(ncycle)
cout<<"-1";
else
{
for(int i=2;i<=N;i++)
{
if(dist[i]!=INF)
cout<<dist[i]<<'\n';
else
cout<<"-1\n";
}
}
}