HDU3986 Harry Potter and the Final Battle

Problem Description

The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.

Input

First line, case number t (t<=20).
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.

Output

Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.

Example Input

3
4
4
1 2 5
2 4 10
1 3 3
3 4 8
3
2
1 2 5
2 3 10
2
2
1 2 1
1 2 2

Example Output

15
-1
2

题意

给定一张图,求删除一条边后最短路最长是多少,如果不可达输出-1

题解

图的范围较小,考虑枚举最短路上的每一条边,修改权值为最大后再跑一遍最短路,保存最大值即可。

  • 由于是双向边,可从起点出发跑一遍最短路记为dis[],从终点出发跑一遍最短路记为rdis[],对于每一条边{x,y,w}, d i s [ x ] + r d i s [ y ] + w = = d i s [ n ] dis[x]+rdis[y]+w==dis[n] dis[x]+rdis[y]+w==dis[n],这条边即为最短路上的边
  • 应该特判最短路不存在时的情况

C++

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<stack>

using namespace std;

const int N=2e5+50;

queue<int> q;
int first[N],nxt[N*2],to[N*2],n,m,len,dis[N],rdis[N],ndis[N],T,c[N];
bool inqueue[N];

struct edge
{
    int x,y,w;
    void init(int a,int b,int c)
    {
        x=a,y=b,w=c;
    }
} g[N];

void addedge(int x,int y,int z)
{
    nxt[++len]=first[x];
    first[x]=len;
    to[len]=y;
    c[len]=z;
}

void spfa(int *dis,int s)
{
    memset(inqueue,false,sizeof(inqueue));

    for (int i=0;i<=n;i++) dis[i]=21474834;
    dis[s]=0;
    inqueue[s]=true;
    q.push(s);

    while (!q.empty())
    {
        int x=q.front();
        q.pop();

        for (int i=first[x];i;i=nxt[i])
        {
            int y=to[i];

            if (dis[x]+c[i]<dis[y])
            {
                dis[y]=dis[x]+c[i];
                if (!inqueue[y])
                {
                    inqueue[y]=true;
                    q.push(y);
                }
            }
        }
        inqueue[x]=false;
    }

}

int main()
{
    ios::sync_with_stdio(false);

    for (cin>>T;T--;)
    {
        memset(first,0,sizeof(first));
        len=0;
        cin>>n>>m;
        for (int x,y,w,i=1;i<=m;i++)
        {
            cin>>x>>y>>w;
            addedge(x,y,w);
            addedge(y,x,w);
            g[i].init(x,y,w);
        }
        spfa(dis,1);
        spfa(rdis,n);

        if (dis[n]==21474834)
        {
            cout<<-1<<endl;
            continue;
        }

        bool flag=false;
        int ans=0;
        for (int i=1;i<=m;i++)
        if (dis[g[i].x]+rdis[g[i].y]+g[i].w==dis[n] or rdis[g[i].x]+dis[g[i].y]+g[i].w==dis[n])
        {
            c[i*2-1]=c[i*2]=214748364;
            spfa(ndis,1);
            if (ndis[n]==ndis[0])
            {
                flag=true;
                break;
            }
            ans=max(ans,ndis[n]);
            c[i*2-1]=c[i*2]=g[i].w;
        }

        if (flag) ans=-1;
        cout<<ans<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值