O

来源:互联网 发布:java方法名 编辑:程序博客网 时间:2024/04/29 15:17
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

256 7 8 9 1061 22 33 41 55 44 5245210 1011 212

Sample Output

Case 1:34Case 2:?

题目思路:由于本题边的权值可能会出现负数的情况,而且路径数不确定,所以是会出现负环的情况。此时我们就无法使用dijkstra来解决本题,只能使用spfa来判断是否存在负环。

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cstring>#include <queue>#define INF 0x3f3f3f3fusing namespace std;struct node{  int v, w, next;}t[121212];//由于题目没有提到具体的数量,开的大一点int head[211];int a[212];bool v[212];int dis[212];int tim[212];//记录入队的次数int top;void add(int u, int v)//加边{   t[top].v = v;   t[top].w = (a[v] - a[u]) * (a[v] - a[u]) * (a[v] - a[u]);   t[top].next = head[u];   head[u] = top++;}void Initial()//初始化{   memset(head, -1, sizeof(head));   memset(v, false, sizeof(v));   memset(dis, INF, sizeof(dis));   memset(tim, 0, sizeof(tim));   top = 1;}void spfa(int n){    dis[1] = 0;    queue<int> q;    q.push(1);    v[1] = true;    tim[1]++;    while(!q.empty())    {       int p = q.front();       q.pop();       v[p] = false;//记录是否入队       for(int l=head[p];l!=-1;l=t[l].next)       {            if(dis[t[l].v]>dis[p] + t[l].w)            {               dis[t[l].v] = dis[p] + t[l].w;               if(!v[t[l].v])               {                  v[t[l].v] = true;                  q.push(t[l].v);                  tim[t[l].v]++;                  if(tim[t[l].v]>=n)//如果入队的次数大于总的点数,则视为出现负环                  {                      return ;                  }               }            }       }    }}int main(){   int n, m, T, q, v, u;   scanf("%d", &T);   for(int tt=1;tt<=T;tt++)   {      scanf("%d", &n);      for(int i=1;i<=n;i++)      scanf("%d", &a[i]);      Initial();      scanf("%d", &m);      for(int i=1;i<=m;i++)      {      scanf("%d %d", &u, &v);      add(u, v);      }      spfa(n);//首先处理从1到其他个点的距离      scanf("%d", &q);      cout<<"Case "<<tt<<':'<<endl;      while(q--)      {        int k;        scanf("%d", &k);        if(dis[k]==INF||dis[k]<3)        cout<<'?'<<endl;        else        cout<<dis[k]<<endl;      }   }  return 0;}
原创粉丝点击