-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLOJ-1074.cpp
71 lines (70 loc) · 1.65 KB
/
LOJ-1074.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
#include<bits/stdc++.h>
using namespace std;
#define cube(x) ((x)*(x)*(x))
int n,m;
int arr[202];
vector<pair<int,int> >g[202];
int cost[202];
bool vis[202];
void BF() {
cost[1]=0;
for(int k=0; k<n; k++) {
bool f=1;
for(int i=1; i<=n; i++) {
for(int j=0; j<g[i].size(); j++) {
pair<int,int>dest=g[i][j];
if(dest.first==n && (f=0))cost[n]=0;
if(cost[dest.first]>(cost[i]+dest.second)) {
cost[dest.first]=cost[i]+dest.second;
f=0;
}
}
}
if(f)return;
}
return;
}
void dfs(int src) {
vis[src]=1;
for(int i=0; i<g[src].size(); i++) {
if(!vis[g[src][i].first]) {
dfs(g[src][i].first);
}
}
return;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
int t;
scanf("%d",&t);
for(int z=1; z<=t; z++) {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%d",&arr[i]);
cost[i]=1<<30;
}
scanf("%d",&m);
for(int i=0; i<m; i++) {
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(make_pair(v,cube(arr[v]-arr[u])));
}
BF();
memset(vis,0,sizeof(vis));
dfs(1);
int q;
scanf("%d",&q);
cout<<"Case "<<z<<":\n";
while(q--) {
int a;
scanf("%d",&a);
if(!vis[a] || cost[a]<3 || cost[a]>=(1<<30)) {
cout<<"?\n";
} else cout<<cost[a]<<"\n";
}
for(int i=0; i<=n; i++)g[i].clear();
}
return 0;
}