【最短路 spaf判断负环】O - Extended Traffic

本文介绍了一种通过算法解决城市交通拥堵问题的方法。利用每个路口的拥挤度来计算从一个路口到另一个路口所需的时间,并考虑了负环的情况。通过SPFA算法判断是否存在更优路径,最终目标是从指定起点到达其他各个路口的最短时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 nintegers 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个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J的时间为:(aJ-aI)^3,存在负环。问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的时间。。

        思路:spaf判断负环

上代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=225;

struct pp
{
    int b,l,ne;
} q[N*N];

int f[N];
int w[N];
int dis[N],mark[N];
int use[N];
int cir[N];
int n;

void dfs(int x)
{
    cir[x]=1;
    for(int i=f[x];i!=-1;i=q[i].ne)
    {
        int v=q[i].b;
        if(!cir[v])
            dfs(v);
    }
}

void spaf()
{
    int u,k,v;
    memset(cir,0,sizeof(cir));
    memset(use,0,sizeof(use));
    memset(dis,inf,sizeof(dis));
    memset(mark,0,sizeof(mark));
    queue<int>que;
    dis[1]=0;
    que.push(1);
    while(!que.empty())
    {
        u=que.front();
        k=f[u];
        que.pop();
        mark[u]=0;
        while(k!=-1)
        {
            v=q[k].b;
            if(dis[v]>dis[u]+q[k].l)
            {
                dis[v]=dis[u]+q[k].l;
                if(mark[v]==0&&cir[v]==0)
                {
                    que.push(v);
                    use[v]++;
                }
                if(use[v]>=n&&!cir[v])
                    dfs(v);
            }
            k=q[k].ne;
        }
    }
}

int main()
{
    int T,M=1;
    scanf("%d",&T);
    while(T--)
    {
        memset(f,-1,sizeof(f));
        int a,b,i,j,m,Q;
        scanf("%d",&n);
        for(i=1; i<=n; i++)
            scanf("%d",&w[i]);
        scanf("%d",&m);
        for(i=0; i<m; i++)
        {
            scanf("%d %d",&a,&b);
            q[i].b=b;
            q[i].l=(w[b]-w[a])*(w[b]-w[a])*(w[b]-w[a]);
            q[i].ne=f[a];
            f[a]=i;
        }
        scanf("%d",&Q);
        printf("Case %d:\n",M++);
        spaf();
        for(i=1; i<=Q; i++)
        {
            scanf("%d",&a);
            if(dis[a]<3||dis[a]==inf||cir[a])
                printf("?\n");
            else
                printf("%d\n",dis[a]);
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值