-
Notifications
You must be signed in to change notification settings - Fork 1
/
10449 - Traffic.cpp
66 lines (66 loc) · 1.36 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
#include<bits/stdc++.h>
using namespace std;
#define inf INT_MAX/3
#define mx 212
int jam[mx];
struct Edge
{
int u;
int v;
int w;
Edge(int _u, int _v, int _w)
{
u = _u;
v = _v;
w = _w;
}
};
int cost[mx];
int main()
{
int n,m,a,b,cs=1,q,d;
while(cin>>n)
{
printf("Set #%d\n",cs++);
vector<Edge>edge;
for(int i=1; i<=n; i++)
{
scanf("%d",&jam[i]);
cost[i] = inf;
}
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&a,&b);
int c = jam[b] - jam[a];
edge.push_back(Edge(a,b,c*c*c));
}
cost[1] = 0;
///
for(int i=1; i<=n+1500; i++)
{
int up = 0;
for(Edge e:edge)
{
if(cost[e.u]==inf)continue;
if(cost[e.u] + e.w < cost[e.v])
{
cost[e.v] = cost[e.u] + e.w;
up++;
}
}
if(up==0)
break;
}
///
scanf("%d",&q);
while(q--)
{
scanf("%d",&d);
if(cost[d]==inf || cost[d]<3)
printf("?\n");
else
printf("%d\n",cost[d]);
}
}
}