-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTIMETRIP.cpp
More file actions
111 lines (84 loc) · 1.68 KB
/
Copy pathTIMETRIP.cpp
File metadata and controls
111 lines (84 loc) · 1.68 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
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
104
105
106
107
108
109
110
111
#include <bits/stdc++.h>
using namespace std;
const int INF = 987654321;
const int M = 98765432;
int V, E;
vector<vector<pair<int, int>>> adj;
bool reachable[101][101];
int bellmanford()
{
for(int k=0; k < V; k++)
for(int i=0; i < V; i++)
for(int j=0; j < V; j++)
reachable[i][j] |= reachable[i][k] && reachable[k][j];
vector<int> upper(V, INF);
upper[0] = 0;
bool updated;
bool cycle = false;
for(int i=0; i < V; i++)
{
updated = false;
for(int here = 0; here < V; here++)
{
for(size_t j=0; j < adj[here].size(); j++)
{
int there = adj[here][j].first;
int w = adj[here][j].second;
if(upper[there] > upper[here] + w)
{
if(i == V-1 && (reachable[there][1] || reachable[here][1]))
cycle = true;
upper[there] = upper[here] + w;
updated = true;
}
}
}
if(!updated)
break;
}
if(upper[1] >= INF-M)
return -INF;
if(cycle)
return INF;
return upper[1];
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &V, &E);
adj.clear();
adj.resize(V);
memset(reachable, false, sizeof(reachable));
for(int i=0; i < V; i++)
reachable[i][i] = true;
for(int i=0; i < E; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adj[u].push_back({v, w});
reachable[u][v] = true;
}
int past = bellmanford();
if(past == -INF)
{
puts("UNREACHABLE");
continue;
}
if(past == INF)
printf("INFINITY ");
else
printf("%d ", past);
for(size_t i=0; i < V; i++)
for(size_t j=0; j < adj[i].size(); j++)
adj[i][j].second = -adj[i][j].second;
int present = bellmanford();
if(present == INF)
printf("INFINITY\n");
else
printf("%d\n", -present);
}
return 0;
}