Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.
InputInput starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains nintegers denoting the busyness of the junctions from 1 to n respectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.
OutputFor each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a '?'.
Sample Input2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
2
10 10
1
1 2
1
2
Sample OutputCase 1:
3
4
Case 2:
?
题意:题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J的时间为:(aJ-aI)^3,存在负环。问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的时间。。
思路:spaf判断负环
上代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=225;
struct pp
{
int b,l,ne;
} q[N*N];
int f[N];
int w[N];
int dis[N],mark[N];
int use[N];
int cir[N];
int n;
void dfs(int x)
{
cir[x]=1;
for(int i=f[x];i!=-1;i=q[i].ne)
{
int v=q[i].b;
if(!cir[v])
dfs(v);
}
}
void spaf()
{
int u,k,v;
memset(cir,0,sizeof(cir));
memset(use,0,sizeof(use));
memset(dis,inf,sizeof(dis));
memset(mark,0,sizeof(mark));
queue<int>que;
dis[1]=0;
que.push(1);
while(!que.empty())
{
u=que.front();
k=f[u];
que.pop();
mark[u]=0;
while(k!=-1)
{
v=q[k].b;
if(dis[v]>dis[u]+q[k].l)
{
dis[v]=dis[u]+q[k].l;
if(mark[v]==0&&cir[v]==0)
{
que.push(v);
use[v]++;
}
if(use[v]>=n&&!cir[v])
dfs(v);
}
k=q[k].ne;
}
}
}
int main()
{
int T,M=1;
scanf("%d",&T);
while(T--)
{
memset(f,-1,sizeof(f));
int a,b,i,j,m,Q;
scanf("%d",&n);
for(i=1; i<=n; i++)
scanf("%d",&w[i]);
scanf("%d",&m);
for(i=0; i<m; i++)
{
scanf("%d %d",&a,&b);
q[i].b=b;
q[i].l=(w[b]-w[a])*(w[b]-w[a])*(w[b]-w[a]);
q[i].ne=f[a];
f[a]=i;
}
scanf("%d",&Q);
printf("Case %d:\n",M++);
spaf();
for(i=1; i<=Q; i++)
{
scanf("%d",&a);
if(dis[a]<3||dis[a]==inf||cir[a])
printf("?\n");
else
printf("%d\n",dis[a]);
}
}
return 0;
}