LightOJ - 1074 Extended Traffic (最短路- spfa判断负环)

Extended Traffic

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条单向路,为了缓解城市的拥挤,规定每个人从一个点前往另一个点都得走最短路径。现在给每个点都定义了一个繁忙程度,而你从a点前往b点所需要花费的时间为(b的繁忙程度 - a的繁忙程度)^3;现在有q次查询,每次给出一个目的地,你始终是从坐标为1的点出发,问从点1到目的地所需要花费的最少时间是多少,如果无法到达或者花费的时间小于3的话输出“?”。

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


AC代码如下:

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<'['<<x<<']'<<endl
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int MX = 200 + 10;

struct edge
{
    int v,w;
    int nxt;
}E[MX*MX];//因为本题并没有给出边的数目,所以最保险就开n*n的大小,保证任意两点之间均可连边;
int head[MX],tot;
int busy[MX];
int T,n,m,q;

void init(){
    memset(head,-1,sizeof(head));
    tot = 0;
}

void add(int u,int v,int w){
    E[tot].v = v;
    E[tot].w = w;
    E[tot].nxt = head[u];
    head[u] = tot++;
}

int dis[MX],vis[MX];
int time[MX];//此处是和普通的spfa最大的不同之处,用来统计每个点的入队次数来判断是否出现负环;

void spfa(int s){
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(time,0,sizeof(time));
    dis[s] = 0;
    time[s]++;
    queue<int>q;
    q.push(s);

    while(!q.empty()){
        int u = q.front();q.pop();vis[u] = 0;

        for(int i = head[u];~i;i = E[i].nxt){
            int v = E[i].v;
            if(dis[v] > dis[u] + E[i].w){
                dis[v] = dis[u] + E[i].w;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);

                    if(++time[v] >= n)//如果出现一个点的入队次数大于总点数就说明出现了负环。
                        return;
                }
            }
        }
    }
}

int main(){
    //FIN;
    scanf("%d",&T);
    int cas = 1;
    while(T--){
        init();
        scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d",&busy[i]);
        }
        scanf("%d",&m);
        for(int i = 0;i < m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            int w = (busy[v]-busy[u])*(busy[v]-busy[u])*(busy[v]-busy[u]);
            add(u,v,w);//邻接表建图;
        }
        scanf("%d",&q);
        printf("Case %d:\n",cas++);
        spfa(1);//先将从点1到其他点的最短路预处理出来;
        while(q--){
            int x;
            scanf("%d",&x);
            if(dis[x] == INF || dis[x] < 3)
                puts("?");
            else
                printf("%d\n",dis[x]);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值