O - Extended Traffic(判断负环)(spfa)(邻接表)

该题目描述了一个基于城市拥挤度的交通问题,其中道路导致的时间成本为两城市拥挤度差的立方。由于存在负环,需要找到所有负环并计算从起点到特定目的地的最短总时间。如果无法到达或时间小于3,则输出'?'。解决方案涉及使用SPFA算法来处理负权边,并通过判断进入队列的次数来确定是否存在负环。
摘要由CSDN通过智能技术生成

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

这道题存在负环,而且所以要求出来所有负环(包括负环能到达的位置),若大于点N,说明不能到达,可以用进队列的次数来判断

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:

?

#include <iostream>
#include <cstdio>
#include <cstring>
#include<stdio.h>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;

const int maxn = 100005;
const int maxm = 205;
const int oo = 0xfffffff;

struct node
{
    int u, v, c, next;
}e[maxn];
int dis[maxm], head[maxm];
int busy[maxn], p[maxn];//p数组记录一共访问的次数
bool use[maxm];

void Add(int u, int v, int c, int k)
{
    e[k].u = u;
    e[k].v = v;
    e[k].c = c*c*c;
    e[k].next = head[u];
    head[u] = k;
}
void spfa(int N)
{
    queue<int> Q;
    Q.push(1);

    while(Q.size())
    {
        int i = Q.front();Q.pop();
        use[i] = false, p[i]++;

        for(int j=head[i]; j!=0; j=e[j].next)
        {
            int u = e[j].u, v = e[j].v, c = e[j].c;

            if(dis[u]+c < dis[v])
            {
                dis[v] = dis[u]+c;

                if(use[v] == false && p[v] <= N)
                {
                    use[v] = true;
                    Q.push(v);
                }
            }
        }
    }
}

int main()
{
    int T, t=1;

    scanf("%d", &T);

    while(T--)
    {
        int i, N, M, u, v;

        scanf("%d", &N);

        for(i=1; i<=N; i++)
        {
            dis[i] = oo;
            head[i] = 0;
            p[i] = 0;
            scanf("%d", &busy[i]);
        }

        scanf("%d", &M);

       for(int i=1;i<=M;i++)
        {
            scanf("%d%d", &u, &v);
            Add(u, v, busy[v]-busy[u], i);
        }

        scanf("%d", &M);
        printf("Case %d:\n", t++);

        dis[1] = 0;
        spfa(N);

        while(M--)
        {
            scanf("%d", &v);

            if(dis[v] == oo || dis[v] < 3 || p[v] > N)
                printf("?\n");
            else
                printf("%d\n", dis[v]);
        }
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值