Extended Traffic LightOJ - 1074 SPFA解决负权回路

Extended Traffic LightOJ - 1074

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个点,1--n的顺序给出每个点都有一定的拥挤值,然后给你m条边,这m条边表示相连着,着m条边的边权值是目的地点的值减去出发点的值的3次方,注意,这是有向图,然后再给你q个点,去求1号点到这q个点的最小值,如果值小于3,或者不能够到达,那就输出 ‘?’

思路:上来先写一发SPFA,不好意思WA了,没有考虑到负环问题,如果给出的边权是   1-->3是1,3-->2是27   ,2-->1是-64

那就会进入死循环,因为我们知道最多进行n-1轮松弛就可以找到最短路在Bellman中,SPFA就是Bellman 的队列优化,所以每个点最多进入到队列中n次就可以找到最短路,如果是进入到队列里面大于等于n次就说明存在负权回路,在这道题目中,负权回路上的所有的点都是不满足题意的,所以就输出  "?"

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int INF=0x3f3f3f3f;
struct node
{
    int u,v,w;
}num[55500];
int dis[300];
int first[300];
int next1[55000];
bool vis[250];
int a[300];
int b[250];
int n,m;
int p;
int e[205];
bool flag[250];
void dfs(int x)
{
    flag[x]=1;
    int k=first[x];
    while(k!=-1)
    {
        if(!flag[num[k].v])
        {

            flag[num[k].v]=1;
            dfs(num[k].v);
        }
            k=next1[k];
    }
    return;
}
void spfa()
{
    memset(dis,INF,sizeof dis);
    memset(vis,0,sizeof vis);
    memset(b,0,sizeof b);
    memset(flag,0,sizeof flag);
    queue<int>q;
    q.push(1);
    dis[1]=0;vis[1]=1;
    b[1]++;
    while(q.size())
    {
        int k=first[q.front()];
        while(k!=-1)
        {
            if(dis[num[k].v]>dis[num[k].u]+num[k].w)
            {
                dis[num[k].v]=dis[num[k].u]+num[k].w;
                if(!vis[num[k].v])
                {
                    if(flag[num[k].v]) continue;
                    b[num[k].v]++;
                    if(b[num[k].v]>=n){

                        dfs(num[k].v);
                    }
                    q.push(num[k].v);
                    vis[num[k].v]=1;
                }
            }
            k=next1[k];
        }
        vis[q.front()]=0;q.pop();
    }
//    rep(i,1,n)
//    printf("%d  ",dis[i]);
//    printf("\n");
    rep(i,1,p)
    if(dis[e[i]]>=3&&dis[e[i]]<INF&&!flag[e[i]])
    printf("%d\n",dis[e[i]]);
    else
        printf("?\n");

}
int main()
{
    int t;
    scanf("%d",&t);
    rep(o,1,t)
    {
        memset(first,-1,sizeof first);
        memset(next1,-1,sizeof next1);
        scanf("%d",&n);
        rep(i,1,n)
        scanf("%d",&a[i]);
        scanf("%d",&m);
        rep(i,1,m)
        {
            scanf("%d%d",&num[i].u,&num[i].v);
            int val=a[num[i].v]-a[num[i].u];
            num[i].w=val*val*val;
            next1[i]=first[num[i].u];
            first[num[i].u]=i;
        }
        scanf("%d",&p);
        rep(i,1,p)
        scanf("%d",&e[i]);
        printf("Case %d:\n",o);
        spfa();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/82083664