-
Notifications
You must be signed in to change notification settings - Fork 3
/
lightoj 1074.cpp
125 lines (108 loc) · 2.21 KB
/
lightoj 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Created by ash_98
#include<bits/stdc++.h>
using namespace std;
#define mx 205
#define ll long long
#define mod 10000000 //998244353
int ar[mx];
bool vis[mx];
ll cost[mx];
int n,m,ii,k;
vector<pair<int,ll>>g[mx];
/*struct Edge
{
int u,v;
ll w;
Edge(){};
Edge(int u,int v,int w)
{
this->u=u;
this->v=v;
this->w=w;
}
};
vector<Edge>E,edge;
ll dist[mx];
bool bellman_ford()
{
/// here i can start from 1 .if given that stating node i can set dist[src]=0
for(int i=1;i<=n;i++)dist[i]=mod;
dist[1]=0;
for(int i=1;i<n;i++)
for(Edge it: E)
if(dist[it.v]>dist[it.u]+it.w)
dist[it.v]=dist[it.u]+it.w;
for(Edge it:E)
if(dist[it.v]>dist[it.u]+it.w)return true;
return false;
}
void dfs(int u){
vis[u]=1;
for(auto[v,w]:g[u]){
if(vis[v])continue;
dfs(v);
}
}*/
void solve()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&ar[i]),cost[i]=mod,vis[i]=0,g[i].clear();
scanf("%d",&m);
/* edge.clear();
E.clear();*/
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
ll dif=ar[y]-ar[x];
dif*=dif*dif;
g[x].push_back({y,dif});
// edge.push_back(Edge(x,y,dif));
}
/* dfs(1);
for(auto it:edge){
if(vis[it.u])E.push_back(it);
}*/
printf("Case %d:\n",++ii);
/*if(bellman_ford()){
int q;
scanf("%d",&q);
while(q--){
int x;
scanf("%d",&x);
printf("?\n");
}
return;
}*/
priority_queue<pair<ll,int>>pq;
pq.emplace(0,1);
while(!pq.empty()){
auto[w,u]=pq.top();
pq.pop();
w*=-1;
if(w<=-8000)continue;
if(cost[u]<w)continue;
for(auto[v,c]:g[u]){
if(cost[v]>c+w){
cost[v]=c+w;
pq.push({-cost[v],v});
}
}
}
// cout<<"reach";
int q;
scanf("%d",&q);
while(q--){
int x;
scanf("%d",&x);
if(cost[x]>=mod or cost[x]<3)
printf("?\n");
else printf("%lld\n",cost[x]);
}
}
int main()
{
int t=1;
scanf("%d",&t);
while(t--)solve();
return 0;
}