Light oj 1074 - Extended Traffic

来源:互联网 发布:任务网站源码 编辑:程序博客网 时间:2024/05/17 03:21

1074 - Extended Traffic
   PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

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.

Input

Input 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 n integers 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.

Output

For 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 Input

Output for Sample Input

2

 

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

Case 1:

3

4

Case 2:

?

 


PROBLEM SETTER: MUSTAQ AHMED
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)

题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J的时间为:(aJ-aI)^3,存在负环。问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的时间。。

思路:用spfa判负环


#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<queue>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x3f#define eps 1e-6#define inf 0x3f3f3f3fusing namespace std;#define maxn 210#define maxm 40010int value[maxn];int dis[maxn];int u[maxm];int v[maxm];int w[maxm];int first[maxn];int conut[maxn];int circle[maxn];int vis[maxn];int nex[maxm];int ans;int case_=1;void add_edge(int u_,int v_,int w_){    u[ans]=u_,v[ans]=v_,w[ans]=w_;    nex[ans]=first[u_];    first[u_]=ans;    ans++;}int n;int m;int k;void dfs(int x){    circle[x]=1;    for(int i=first[x];i!=-1;i=nex[i])    {        if(!circle[v[i]])            dfs(v[i]);    }}void spfa(int x){    queue<int > Q;    while(Q.size())        Q.pop();    memset(vis,0,sizeof(vis));    memset(dis,INF,sizeof(dis));    memset(conut,0,sizeof(conut));    memset(circle,0,sizeof(circle));    Q.push(x);    vis[x]=1;    dis[x]=0;    conut[x]++;    while(!Q.empty())    {        int t=Q.front();        Q.front();        Q.pop();        vis[t]=0;        for(int i=first[t];i!=-1;i=nex[i])        {            if(dis[v[i]]>dis[t]+w[i])            {                dis[v[i]]=dis[t]+w[i];                if(!vis[v[i]] && !circle[v[i]])                    Q.push(v[i]),conut[v[i]]++;               if(conut[v[i]]>=n && !circle[v[i]])                    dfs(v[i]);            }        }    }}int main(){    int T;    cin>>T;    while(T--)    {        ans=0;        memset(first,-1,sizeof(first));        scanf("%d",&n);        for(int i=1;i<=n;i++)            scanf("%d",&value[i]);        scanf("%d",&m);        while(m--)        {            int aa,bb;            scanf("%d%d",&aa,&bb);            add_edge(aa,bb,(value[bb]-value[aa])*(value[bb]-value[aa])*(value[bb]-value[aa]));        }        spfa(1);        scanf("%d",&k);        printf("Case %d:\n",case_++);        while(k--)        {            int aa;            scanf("%d",&aa);            if(circle[aa] || dis[aa]<3 || dis[aa]==inf)               printf("?\n");            else            printf("%d\n",dis[aa]);        }    }    return 0;}



1 0