-
Notifications
You must be signed in to change notification settings - Fork 1
/
558 - Wormholes.cpp
57 lines (46 loc) · 1.13 KB
/
558 - Wormholes.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
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int,int> ;
using vec = vector< vector<pii> > ;
const int INF = 1e9 + 90;
void solve(){
int node, edge;
scanf("%d%d",&node,&edge);
vec graph(node+1);
for(int i= 0; i < edge ; i++){
int u , v, cost;
scanf("%d%d%d",&u,&v,&cost);
graph[u].emplace_back(v,cost);
}
vector<int> dist(node+1,INF);
dist[0]=0;
for(int i = 1; i < node ; i++){
for(int j = 0 ; j < node; j++){
for(auto x : graph[j]){
dist[x.first] = min(dist[x.first],
dist[j]+x.second);
if(dist[x.first] < 0){
puts("possible");
return ;
}
}
}
}
for(int j = 0 ; j < node ; j++){
for(auto x : graph[j]){
if(dist[x.first]> dist[j]+x.second){
puts("possible");
return ;
}
}
}
puts("not possible");
return ;
}
int main(){
int testcase;
scanf("%d",&testcase);
while(testcase--)
solve();
return 0;
}