Extended Traffic

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 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.

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

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

Sample Output

Case 1:

3

4

Case 2:

?

题目大意:题意:n个城市,每个城市都有拥挤度,a点到b点的时间是(b拥挤度-a拥挤度)^3,第一个点是初始点,求最短路。
若不可能到达,或者小于3,就输出“?”
解题思路:建图,求最短路。
注意可能会有负权回路,用spfa

#include 
#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f
using namespace std;
int dis[1000005];//到起始点的距离
int head[1000005];//每个节点作为起点所连的边中的最后输入的边
bool vis[1000005];//标记是否在队列中
int book[300];
int p[300];
int n,m;
struct node
{
    int from,to,w,next;//每个节点的起始点终点和权,next指向拥有相同起点的上一个输入的边的编号
}edge[1000005];//边集
void spfa(int Begin)
{
    int i;
    queue q;//这里用栈可以减少时间(stack q;)
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[Begin]=0;
    q.push(Begin);
    vis[Begin]=1;
    int u;
    while(!q.empty())
    {
        u=q.front();//如果用栈,这里改为(u=q.top();)
        q.pop();
        vis[u]=0;
        for(i=head[u];i!=-1;i=edge[i].next)//到了-1,证明再无相同起点的边
        if(dis[edge[i].to]>dis[u]+edge[i].w)//对于所有拥有相同起点的边松弛
        {
            dis[edge[i].to]=dis[u]+edge[i].w;
            if(!vis[edge[i].to]&&book[edge[i].to]<=n)//如果终点不在队列中,就加进去
            {
                vis[edge[i].to]=1;
                q.push(edge[i].to);
                book[edge[i].to]++;
            }
        }
    }
}

int T;
void addedge(int u,int v,int w)//用结构体存边,结构体下标代表边的编号
{
    edge[T].from=u;
    edge[T].to=v;
    edge[T].w=w;
    edge[T].next=head[u];
    head[u]=T++;
}
int main()
{
    int i,t,u,v,m,End,flag=0;;
    scanf("%d",&t);
    while(t--)
    {
        flag++;
        memset(book,0,sizeof(book));
        memset(head,-1,sizeof(head));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&p[i]);
        }
        T=0;
        scanf("%d",&m);
        for(i=1; i<=m; i++)
        {
            scanf("%d %d",&u,&v);
            addedge(u,v,(p[v]-p[u])*(p[v]-p[u])*(p[v]-p[u]));
        }
        spfa(1);
        int nn;
        scanf("%d",&nn);
        printf("Case %d:\n",flag);
        for(i=1;i<=nn;i++)
        {
            scanf("%d",&End);
            if(dis[End]!=inf&&dis[End]>=3&&book[End]<=n)
                printf("%d\n",dis[End]);
            else
                printf("?\n");
        }
    }
    return 0;
}

 

你可能感兴趣的:(最短路,匡斌专题四最短路,spfa)