[kuangbin]专题四 最短路练习 Extended Traffic LightOJ - 1074【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.
达卡市日益拥挤和嘈杂。拥堵时,某些道路始终受阻。为了说服人们避开最短的路线,以及拥挤的道路,到达目的地,市政当局制定了新的计划。城市的每个交叉点都标有正整数(≤20),表示交叉点的繁忙程度。每当有人从一个路口(源路口)到另一个路口(目的地路口)时,市政府就会从旅行者那里获得金额(目的地的繁忙程度 - 来源的繁忙程度)3(这意味着差异的立方)。当局已经指定你找出当某人智能从某个交叉点(零点)到其他几个交汇点时可以获得的最低总金额。

【输入】
Input starts with an integer T (≤ 50), denoting the number of test cases.
输入以整数T(≤50)开始,表示测试用例的数量。
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.
每个案例都包含一个空行和一个表示结点数的整数n(1 <n≤200)。下一行包含n个整数,分别表示从1到n的连接的繁忙程度。下一行包含一个整数m,即城市中道路的数量。接下来的m行中的每一行(每条道路一条)包含相应道路连接的两个交叉点(源,目的地)(所有道路都是单向的)。下一行包含整数q,即查询数。下一个q行每个都包含一个目标结点号。从交叉路口到另一个交叉路口最多只能有一条直达道路。

【输出】
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 ‘?’.
对于每种情况,请在一行中打印案例编号。然后打印q行,每个查询一行,每行包含从结点1(零点)到达给定结点的最小总收入。但是,对于总收入小于3的查询,或者如果无法从零点到达目的地,则打印“?”。

【样例输入】
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

【样例输出】
Case 1:
3
4
Case 2:
?

题目链接:https://cn.vjudge.net/problem/LightOJ-1074

spfa最短路,负环上dfs标记

代码如下:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=200;
struct Edge{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN+10];
bool bad[MAXN+10];
bool vis[MAXN+10];
int dist[MAXN+10];
int cnt[MAXN+10];
void dfs(int v)
{
    bad[v]=true;
    for(int i=0;i<E[v].size();i++)
        if(!bad[E[v][i].v])
            dfs(E[v][i].v);
}
void spfa(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dist[i]=INF;
        cnt[i]=0;
        bad[i]=false;
    }
    vis[start]=true;
    dist[start]=0;
    cnt[start]=1;
    queue<int> Q;
    Q.push(start);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        vis[u]=false;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            int cost=E[u][i].cost;
            if(!bad[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                if(!vis[v])
                {
                    vis[v]=true;
                    Q.push(v);
                    if(++cnt[v]>n) dfs(v);
                }
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int x[MAXN+10];
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    for(int kase=1;kase<=T;kase++)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>x[i];
        for(int i=1;i<=n;i++)
            E[i].clear();
        int m;
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            int a,b;
            cin>>a>>b;
            addedge(a,b,(x[b]-x[a])*(x[b]-x[a])*(x[b]-x[a]));
        }
        cout<<"Case "<<kase<<":"<<endl;
        spfa(n,1);
        int k;
        cin>>k;
        for(int i=1;i<=k;i++)
        {
            int st;
            cin>>st;
            if(!bad[st] && dist[st]!=INF && dist[st]>=3)
                cout<<dist[st]<<endl;
            else
                cout<<"?"<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值