1. 程式人生 > >1074 Extended Traffic(spfa+判負環)

1074 Extended Traffic(spfa+判負環)

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 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個城市,每個城市都有一個值,兩個城市之間的距離為(目的地的值-出發點的值)^3,接下來有m條單向邊,再接下來t組詢問,問從1號城市到x城市的最短路徑是多少,如果無法到達或最短路徑距離小於3則輸出‘?’,否則輸出最短路徑值。

題解:

該題難點在於負環上。

負環概念:權重為負值的環稱為負環,當一張圖有負環,只要不斷去繞行負環,「最短途徑」的長度將是無限短。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const int maxn=80000+10;
int val[205];
struct cc{
    int from,to,cost;
}es[maxn];
int first[maxn],nxt[maxn];
int tot=0;
void build(int ff,int tt,int pp)
{
    es[++tot]=(cc){ff,tt,pp};
    nxt[tot]=first[ff];
    first[ff]=tot;
}
bool vis[maxn];
int dis[maxn];
int cir[maxn];//記錄哪些點在負環中
int num[maxn];//記錄點的入隊次數
queue<int>q;
void dfs(int x)
{
    for(int i=first[x]; i; i=nxt[i])
    {
        int v=es[i].to;
        if(!cir[v])
        {
            cir[v]=1;
            dfs(v);
        }
    }
}
int n;
void spfa(int s)
{
    q.push(s);
    vis[s]=1;
    dis[s]=0;
    num[s]++;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=first[u]; i; i=nxt[i])
        {
            int v=es[i].to;
            if(dis[v]>dis[u]+es[i].cost)
            {

                dis[v]=dis[u]+es[i].cost;
                if(!vis[v]&&!cir[v])
                {
                    vis[v]=1;
                    q.push(v);
                    num[v]++;
                    if(num[v]>n)//如果該點入隊超過n次,則該點在負環上。
                    {
                        cir[v]=1;
                        dfs(v);//將負環中的點全部標記
                    }
                }
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int k=1; k<=T; k++)
    {
        printf("Case %d:\n",k);
        scanf("%d",&n);
        tot=0;
        memset(first,0,sizeof(first));
        memset(nxt,0,sizeof(nxt));
        memset(dis,63,sizeof(dis));
        memset(cir,0,sizeof(cir));
        memset(num,0,sizeof(num));
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&val[i]);
        }
        int m;
        scanf("%d",&m);
        for(int i=1; i<=m; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            int cha=val[y]-val[x];
            build(x,y,cha*cha*cha);
        }
        spfa(1);
        int t;
        scanf("%d",&t);
        for(int i=1; i<=t; i++)
        {
            int x;
            scanf("%d",&x);
            if(dis[x]==dis[0]||dis[x]<3||cir[x])
            {
                printf("?\n");
            }
            else
            {
                printf("%d\n",dis[x]);
            }
        }
    }
    return 0;
}