-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtendedTraffic.cpp
88 lines (78 loc) · 1.95 KB
/
ExtendedTraffic.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
79
80
81
82
83
84
85
86
87
88
/* http://www.lightoj.com/volume_showproblem.php?problem=1074
#bellman-ford
*/
#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, vector<int>& dist) {
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=1; t < testcases + 1; t++) {
int n,m;
cin >> n;
int busynessArr[n+1];
for(int i=1; i < n+1; i++){
int tmp;
cin >> tmp;
busynessArr[i] = tmp;
}
cin >> m;
vector<Edge> graph;
for(int i=0; i < m; i++){
int u, v;
cin >> u >> v;
int w = busynessArr[v] - busynessArr[u];
Edge e(u, v, w*w*w);
graph.push_back(e);
}
vector<int> dist(n+1, INT_MAX);
dist[1] = 0;
bool r = BellmanFord(graph, n, dist);
cout << "Case " << t << ":" << endl;
int q;
cin >> q;
for (int i=0; i < q; i++) {
int num;
cin >> num;
if( dist[num] < 3 || dist[num] == INT_MAX) {
cout << "?" << endl;
} else {
cout << dist[num] << endl;
}
}
}
return 0;
}