-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path558-Wormholes.cpp
78 lines (58 loc) · 1.35 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <string.h> // memset
#include <algorithm> // lower_bound
using namespace std;
#define fs first
#define sd second
#define pb push_back
#define MAXN 1020
const int INF = 0x3f3f3f3f;
struct edge {
int from, to, value;
};
int n, m, c;
edge e;
vector<edge> Edge;
void bellmanFord() {
int flag = 0;
vector<int> distance (n + 1, INF);
distance[0] = 0;
// relaxion process
for(int i = 0; i < n; ++i) {
flag = 0;
// for all edges
for(int j = 0; j < m;++j) {
// if this vertice is reachble
if(distance[ Edge[j].from ] < INF) {
if(distance[ Edge[j].to ] > distance[ Edge[j].from] + Edge[j].value) {
distance[ Edge[j].to ] = distance[ Edge[j].from] + Edge[j].value;
flag = 1;
}
}
}
}
if(flag)
cout << "possible\n";
else
cout << "not possible\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> c;
while(c--) {
cin >> n >> m;
for(int i = 0; i < m; ++i) {
cin >> e.from >> e.to >> e.value;
Edge.pb(e);
}
bellmanFord();
Edge.clear();
}
return 0;
}