lightoj 1074 spfa判断负环

 Extended Traffic
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

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个城市,m条路,每条路连接2个城市,费用为a[y] - a[x]的3次方。问对于每个点从1出发的费用。

思路:这题存在负环,所以可以用spfa解决,对于负环内的点我们要进行标记,下次遇到的时候就可以直接跳过

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = 210;
struct node
{
    int to;
    ll val;
    int next;
}edge[MAXN*MAXN*2];
int ind,pre[MAXN],vis[MAXN],n,m,dis[MAXN],a[MAXN],num[MAXN];
void add(int x,int y,int z)
{
    edge[ind].to = y;
    edge[ind].val = z;
    edge[ind].next = pre[x];
    pre[x] = ind ++;
}
void spfa()
{
    for(int i = 0; i <= n; i++){
        dis[i] = INF;
        vis[i] = 0;
        num[i] = 0;
    }
    num[1] = 1;
    vis[1] = 1;
    dis[1] = 0;
    queue<int>q;
    q.push(1);
    while(!q.empty()){
        int tp = q.front();
        q.pop();
        vis[tp] = 0;
        for(int i = pre[tp]; i != -1; i = edge[i].next){
            int t = edge[i].to;
            if(num[t] > n)continue;
            if(dis[t] > dis[tp] + edge[i].val){
                dis[t] = dis[tp] + edge[i].val;
                if(!vis[t]){
                    num[t] ++;
                    vis[t] = 1;
                    q.push(t);
                }
            }
        }
    }
}
int power(int x)
{
    return x * x * x;
}
int main()
{
    int t,ff = 0;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
        }
        scanf("%d",&m);
        ind = 0;
        memset(pre,-1,sizeof(pre));
        int x,y;
        for(int i = 1; i <= m; i++){
            scanf("%d%d",&x,&y);
            add(x,y,power(a[y] - a[x]));
        }
        spfa();
        int q;
        scanf("%d",&q);
        printf("Case %d:\n",++ff);
        while(q--){
            scanf("%d",&x);
            if(dis[x] < 3 || dis[x] >= INF || num[x] > n){
                printf("?\n");
            }
            else {
                printf("%d\n",dis[x]);
            }
        }
    }
    return 0;
}

 

posted @ 2016-05-06 20:34  sweat123  阅读(179)  评论(0编辑  收藏  举报