-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLightOJ 1108.cpp
103 lines (91 loc) · 1.77 KB
/
LightOJ 1108.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
#include <bits/stdc++.h>
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define rep(i,n) for(int i = 0 ; i < (n) ; i++)
#define iter(i,a,b) for(int i = (a) ; i < (b) ; i++)
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,pii> iii;
typedef vector <pii> vii;
#define INF 1000000000
#define MAXX 1005
vector< pii > adj_l[MAXX];
int dist[MAXX], V, E;
bool vis[MAXX];
vector< int > ans, AdjList[MAXX];
void init(){
rep(i, MAXX){
adj_l[i].clear();
AdjList[i].clear();
dist[i] = INF;
vis[i] = false;
}
ans.clear();
}
void input(int u, int v, int w){
adj_l[v].push_back(make_pair(w, u));
AdjList[v].push_back(u);
}
void bellman_ford(int src){
dist[src] = 0;
rep(i, V - 1){
rep(u, V){
rep(j, (int)adj_l[u].size()){
pii v = adj_l[u][j];
dist[v.second] = min(dist[v.second], dist[u] + v.first);
}
}
}
}
void dfs(int src){
vis[src] = true;
ans.push_back(src);
rep(i, AdjList[src].size()){
int v = AdjList[src][i];
if(!vis[v]){
dfs(v);
}
}
}
bool negCycle(){
bellman_ford(0);
bool hasNegCycle = false;
rep(u, V){
rep(j, (int)adj_l[u].size()){
pii v = adj_l[u][j];
if(dist[v.second] > (dist[u] + v.first)){
hasNegCycle = true;
dfs(v.second);
}
}
}
return hasNegCycle;
}
int main(){
//READ("LightOJ 1108.txt");
int tc, case_no = 0, u, v, w;
cin>>tc;
while(tc--){
init();
cin>>V>>E;
rep(i, E){
cin>>u>>v>>w;
input(u, v, w);
}
printf("Case %d:", ++case_no);
if(negCycle()){
sort(ans.begin(), ans.end());
ans.erase( unique( ans.begin(), ans.end() ), ans.end() );
rep(i, ans.size()){
printf(" %d", ans[i]);
}
printf("\n");
}
else if(!negCycle()){
printf(" impossible\n");
}
}
return 0;
}