-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 10449 - Traffic.cpp
77 lines (73 loc) · 1.92 KB
/
UVA 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
#include <bits/stdc++.h>
#define ii pair<int, int>
#define inf 1000000000000
using namespace std;
vector< vector<ii> > adj(210);
long long dist[210];
int n, cost[210];
bool reachable[210];
void BellmanFord()
{
for(int i=2; i<=n; i++) dist[i] = inf;
dist[1] = 0;
ii p;
bool isUpdated = 0;
for(int i=0; i<n-1; i++){
isUpdated = 0;
for(int u=1; u<=n; u++){
for(int j=0; j<adj[u].size(); j++){
p = adj[u][j];
if( dist[p.first] > dist[u] + p.second && dist[u] != inf ){
dist[p.first] = dist[u] + p.second;
isUpdated = 1;
}
}
}
if(!isUpdated) break;
}
memset(reachable, 0, sizeof reachable);
queue<int> q;
for(int u=1; u<=n; u++){
for(int j=0; j<adj[u].size(); j++){
p = adj[u][j];
if(dist[p.first] > dist[u] + p.second && dist[u] != inf ){
q.push(p.first);
}
}
}
int t;
while(!q.empty()){
t = q.front();
q.pop();
if(reachable[t]) continue;
reachable[t] = 1;
for(int i=0; i<adj[t].size(); i++)
q.push( adj[t][i].first );
}
}
int main()
{
int tc = 1;
while(scanf("%d", &n) == 1){
for(int i=1; i<=n; i++) adj[i].clear();
for(int i=1; i<=n; i++) scanf("%d", &cost[i] );
int r;
scanf("%d", &r);
int a, b, temp;
for(int i=0; i<r; i++){
scanf("%d%d", &a, &b);
temp = cost[b] - cost[a];
temp = temp * temp * temp;
adj[a].push_back( ii(b, temp) );
}
int q;
scanf("%d", &q);
printf("Set #%d\n",tc++);
BellmanFord();
for(int i=0; i<q; i++){
scanf("%d", &a);
if(dist[a] < 3 || dist[a] == inf || reachable[a] ) printf("?\n");
else printf("%lld\n", dist[a]);
}
}
}