-
Notifications
You must be signed in to change notification settings - Fork 0
/
1074 - Extended Traffic.cpp
90 lines (71 loc) · 1.59 KB
/
1074 - Extended Traffic.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
89
90
//
// Created by @mhhrakib on 4/18/18.
//
#include<bits/stdc++.h>
using namespace std;
const int N = 201;
const int inf = 1 << 28;
struct edge {
int u, v, w;
edge() {}
edge(int _u, int _v, int _w) {
u = _u;
v = _v;
w = _w;
}
};
int dist[N], busy[N];
void Bellman_Ford(int s, int nodes, vector<edge> E) {
int i, tmp, cnt;
for (i = 1; i <= nodes; i++)
dist[i] = inf;
dist[s] = 0;
int sz = E.size();
for (i = 1; i < nodes; i++) {
cnt = 0;
for (int j = 0; j < sz; j++) {
edge e = E[j];
tmp = dist[e.u] + e.w;
if (dist[e.v] > tmp) {
dist[e.v] = tmp;
cnt++;
}
}
if (!cnt)
return;
}
}
int cube(int n) {
return n * n * n;
}
int main() {
int t;
scanf("%d", &t);
int n, m, u, v, w, cn = 1, q;
while (t--) {
vector<edge> E;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &busy[i + 1]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
w = cube(busy[v] - busy[u]);
E.push_back(edge(u, v, w));
}
scanf("%d", &q);
Bellman_Ford(1, n, E);
printf("Case %d:\n", cn++);
for (int i = 0; i < q; i++) {
int x;
scanf("%d", &x);
int res = dist[x];
if (res < 3 || res >= 268400000) {
printf("?\n");
} else
printf("%d\n", res);
}
}
return 0;
}