-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstantViewOfBigBang.cpp
164 lines (113 loc) · 2.83 KB
/
instantViewOfBigBang.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//Bismillahir Rahman-ir Rahim
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << '>' << #x << " : " << x << endl;
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef unsigned long long ull;
typedef long long ll;
const int mx = 2005;
const int inf = 1e8+7;
vector < int > adj[mx];
// map < int, vector < int > > adj;
vector < int > dist;
vector < int > vis;
struct Edge{
int u, v, c;
};
vector < Edge > edges;
set < int > ans;
void dfs(int source){
vis[source] = 1;
ans.insert(source);
for(auto v: adj[source]){
if(!vis[v]){
dfs(v);
}
}
}
void bellmanFord(int source, int n){
dist[source] = 0;
vector < int > parent(n);
int lastNode;
for(int i = 0; i < n; i++){
lastNode = -1;
for(auto e: edges){
// if(dist[e.u] < inf){
if(dist[e.u] + e.c < dist[e.v]){
dist[e.v] = dist[e.u] + e.c;
parent[e.v] = e.u;
lastNode = e.v;
}
// }
}
}
for(auto e: edges){
if(dist[e.u] + e.c < dist[e.v]){
dist[e.v] = dist[e.u] + e.c;
if(!vis[e.u]){
dfs(e.u);
}
}
}
// if(lastNode != -1){
// int nodeInCycle = lastNode;
// for(int i = 0; i < n; i++){
// nodeInCycle = parent[nodeInCycle];
// }
// int first = 1;
// int curr = nodeInCycle;
// return curr;
// }
// else return -1;
}
int main(){
int T, cs = 1;
cin >> T;
while(T--){
int n, m;
cin >> n >> m;
dist.resize(n+1);
vis.resize(n+1, 0);
edges.resize(m);
for(int i = 0; i < n; i++){
dist[i] = inf;
}
for(int i = 0; i < m; i++){
int u, v, c;
cin >> u >> v >> c;
// cin >> edges[i].u >> edges[i].v >> edges[i].c;
edges[i].u = v;
edges[i].v = u;
edges[i].c = c;
adj[v].push_back(u);
}
for(int i = 0; i < n; i++){
vis[i] = 0;
}
bellmanFord(0, n);
cout << "Case "<<cs++ << ": ";
if(ans.size()){
for(auto it = ans.begin(); it != ans.end(); it++){
if(it == prev(ans.end(), 1)){
cout << *it << '\n';
}
else cout << *it << " ";
}
}
else{
cout << "impossible\n";
}
dist.clear();
edges.clear();
ans.clear();
// vis.clear();
// adj.clear();
for(int i = 0; i < n; i++){
adj[i].clear();
}
}
return 0;
}