File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ struct node {
4
+ int u;
5
+ int v;
6
+ int wt;
7
+ node (int first, int second, int weight) {
8
+ u = first;
9
+ v = second;
10
+ wt = weight;
11
+ }
12
+ };
13
+
14
+ int main (){
15
+ int N,m;
16
+ cin >> N >> m;
17
+ vector<node> edges;
18
+ for (int i = 0 ;i<m;i++) {
19
+ int u, v, wt;
20
+ cin >> u >> v >> wt;
21
+ edges.push_back (node (u, v, wt));
22
+ }
23
+
24
+ int src;
25
+ cin >> src;
26
+
27
+
28
+ int inf = 10000000 ;
29
+ vector<int > dist (N, inf);
30
+
31
+ dist[src] = 0 ;
32
+
33
+ for (int i = 1 ;i<=N-1 ;i++) {
34
+ for (auto it: edges) {
35
+ if (dist[it.u ] + it.wt < dist[it.v ]) {
36
+ dist[it.v ] = dist[it.u ] + it.wt ;
37
+ }
38
+ }
39
+ }
40
+
41
+ int fl = 0 ;
42
+ for (auto it: edges) {
43
+ if (dist[it.u ] + it.wt < dist[it.v ]) {
44
+ cout << " Negative Cycle" ;
45
+ fl = 1 ;
46
+ break ;
47
+ }
48
+ }
49
+
50
+ if (!fl) {
51
+ for (int i = 0 ;i<N;i++) {
52
+ cout << i << " " << dist[i] << endl;
53
+ }
54
+ }
55
+
56
+
57
+ return 0 ;
58
+ }
You can’t perform that action at this time.
0 commit comments