O - Extended Traffic LightOJ - 1074

O - 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 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:

?

题意:城市有多个路口,每个都有一个值,路口u到v的权值为(a[v] - a[u])的三次方,有q个询问,每次询问从1到x的最短路,如果小于三或者无法到达则输出?,否则输出最短路大小。

题解:由于权值是差的三次方,所以有可能存在负环,负环可以使最短路无限小,在跑最短路的时候把负环全部标记即可。

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
int dis[maxn],head[maxn],inq[maxn],cir[maxn],a[maxn],inqt[maxn],n,m,tot,k,q;
//cir[i]表示i点是否在负环上,inqt[i]表示i点的入队列次数

struct edge
{
    int v;
    int w;
    int next;
}edg[maxn];

int len(int u,int v)
{
    return (a[v] - a[u]) * (a[v] - a[u]) * (a[v] - a[u]);
}

void addnode(int u,int v,int w)//v点已经入队大于n次,所以v点在负环上,与他相连的点也在负环上
{
    edg[tot].v = v;
    edg[tot].w = w;
    edg[tot].next = head[u];
    head[u] = tot++;
}

void dfs(int u)
{
    cir[u] = 1;
    for(int i = head[u];i != -1;i = edg[i].next)
    {
        int v = edg[i].v;
        if(!cir[v]) dfs(v);
    }
}

void SPFA()
{
    memset(dis,inf,sizeof(dis));
    memset(inq,0,sizeof(inq));
    memset(cir,0,sizeof(cir));
    memset(inqt,0,sizeof(inqt));

    queueQ;
    Q.push(1);
    inq[1] = 1;
    dis[1] = 0;
    inqt[1] = 1;

    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        inq[u] = 0;
        for(int i = head[u];i != -1;i = edg[i].next)
        {
            int v = edg[i].v,w = edg[i].w;
            if(cir[v]) continue;//如果已经在负环上就不算
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(!inq[v])
                {
                    Q.push(v);
                    inq[v] = 1;
                    inqt[v]++;
                    if(inqt[v] > n) dfs(v);//标记负环上的点
                }
            }
        }
    }
}

int main()
{
    int t;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        tot = 0;
        memset(head,-1,sizeof(head));

        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
            scanf("%d",&a[i]);
        scanf("%d",&m);
        while(m--)
        {
            int u,v,w;
            scanf("%d %d",&u,&v);
            w = len(u,v);
            addnode(u,v,w);
        }

        SPFA();

        printf("Case %d:\n",++cas);
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d",&q);
            if(cir[q] || dis[q] < 3 || dis[q] == inf) printf("?\n");
            else                                      printf("%d\n",dis[q]);
        }
    }
    return 0;
}

 

你可能感兴趣的:(最短路)