-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathProblem B.cpp
More file actions
65 lines (59 loc) · 1.95 KB
/
Problem B.cpp
File metadata and controls
65 lines (59 loc) · 1.95 KB
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
#include <iostream>
#include <vector>
using namespace std;
#define intPair pair<int, int> // pair -> (node, weight)
const int up = 1001; // maximum number of systems & time
vector<intPair> G[up]; // Graph -> each node holds vector list of weight edges to other nodes
int dist[up]; // distances from source
int bellmanFord (int nodes) // bellman-ford algorithm
{
for (int i=0; i<nodes-1; i++) // loop V-1 times
{
for (int u=0; u<nodes; u++) // for each of the nodes
{
for (auto v: G[u]) // for each of the neighbours of the node
{
int cost = dist[u] + v.second; // find distance from s -> v
if (cost < dist[v.first]) dist[v.first] = cost; // if min, replace
}
}
}
int cycle = 0; // flag for cycle detection
for (int u=0; u<nodes; u++) // loop over all edges once more
{
for (auto v: G[u])
{
int cost = dist[u] + v.second;
if (cost < dist[v.first]) // if there is another decrease, there has to be a cycle
{
cycle = 1;
break;
}
}
}
return cycle; // returns 0 if no cycle, 1 if cycle
}
int main()
{
int testCases; // the number of test cases
cin>>testCases;
for (int i=0; i<testCases; i++) // for each test case
{
int nodes, edges;
cin>>nodes>>edges;
for (int j=0; j<nodes; j++) // clear gra and reset distances
{
G[j].clear();
dist[j] = up;
}
for (int j=0; j<edges; j++) // input for edges
{
int u, v, t;
cin>>u>>v>>t;
G[u].push_back({v, t});
}
dist[0] = 0; // source node distance is always 0
if (bellmanFord(nodes)) cout<<"possible"<<endl; // bellman-ford returns 1 if cycle found
else cout<<"not possible"<<endl;
}
}