-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10449_Traffic.cpp
80 lines (75 loc) · 1.82 KB
/
10449_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
#include<bits/stdc++.h>
using namespace std;
struct node{
int u,v,c;
};
int n,m;
vector<int> cost,dist;
vector<node> e;
vector<bool> cycle;
void bellman_ford(int a){
dist.clear();
dist.resize(n,INT_MAX);
dist[a] = 0;
for(int i = 0; i < n-1; i++){
//bool change = false;
for(int j = 0; j < e.size(); j++){
int s = e[j].u, d = e[j].v, w = e[j].c;
//cout << s << ' ' << d << ' ' << w <<endl;
if(dist[s] == INT_MAX) continue;
int nw = w+dist[s];
if(dist[d] > nw){
dist[d] = nw;
//change = true;
}
}
//if(!change) return;
}
cycle.clear();
cycle.resize(n);
for(int j = 0; j < e.size(); j++){
int s = e[j].u, d = e[j].v, w = e[j].c;
//cout << s << ' ' << d << ' ' << w <<endl;
if(dist[s] == INT_MAX) continue;
int nw = w+dist[s];
if(dist[d] > nw){
dist[d] = nw;
cycle[d] = true;
//change = true;
}
}
}
int main(){
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int test = 1;
while(cin >> n){
cout << "Set #" << test++<< endl;
cost.clear();
cost.resize(n);
for(int& x: cost) cin >> x;
e.clear();
cin >> m;
while(m--){
int u,v;
cin >> u >> v;
u--;v--;
int c = cost[v]-cost[u];
c = c*c*c;
e.push_back({u,v,c});
}
int q;
cin >> q;
if(!n) continue;
bellman_ford(0);
while(q--){
int x;
cin >> x;
x--;
int ans = dist[x];
if(ans == INT_MAX || ans < 3 || cycle[x]) cout << "?\n";
else cout << ans << endl;
}
}
return 0;
}