LightOJ - 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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值