-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wormholes.cpp
69 lines (59 loc) · 1.52 KB
/
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
/* https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=499
#bellman-ford #shortest-path
*/
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
class Edge {
public:
int u, v, w;
Edge() {}
Edge(int u, int v, int w) {
this->u = u;
this->v = v;
this->w = w;
}
};
bool BellmanFord(vector<Edge>& graph, int n, int source) {
vector<int> dist(n, INT_MAX);
dist[source] = 0;
for (int i=1; i <= n -1; i++) {
for(int k=0; k < graph.size(); k++) {
int u = graph[k].u;
int v = graph[k].v;
int w = graph[k].w;
if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
}
}
}
// check chu trinh am
for (int k=0; k < graph.size(); k++) {
int u = graph[k].u;
int v = graph[k].v;
int w = graph[k].w;
if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {
return false;
}
}
return true;
}
int main() {
int testcases;
cin >> testcases;
for (int t=0; t < testcases; t++) {
int n, m;
cin >> n >> m;
vector<Edge> graph;
int u, v, w;
for (int i=0; i < m; i++) {
cin >> u >> v >> w;
Edge e(u, v, w);
graph.push_back(e);
}
bool ret = BellmanFord(graph, n, 0);
string str = (ret == true) ? "not possible" : "possible";
cout << str << endl;
}
}