hdu 3986 Harry Potter and the Final Battle(最短路+枚举删边)

Harry Potter and the Final Battle

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1741    Accepted Submission(s): 487


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”.
 

Sample 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
 

Sample Output
  
  
15 -1 2
题意:给出一个无向图,删除图中的任意一条边,使得从结点1到结点n的最短路径最长。
思路:显然删除的边一定是在结点1到结点n未删边时的最短路径上,我们可以保存未删边时结点1到结点n的最短路径,然后枚举删除最短路径上的每一条边,再求一次最短路径,最后取最大值。当某一次求最短路径时结点1无法到达结点n,则输出-1.
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <cstdlib>
using namespace std;

const int INF=1000000000;
const int maxn=1005;
const int maxm=100005;
struct node
{
    int v,w;
    int next;
}edge[maxm];
typedef pair<int,int> pii;
int head[maxn],dis[maxn],pedge[maxm],pnode[maxn];
bool vis[maxn];
int n,m,num;
void init()
{
    for(int i=1;i<=n;i++)
    head[i]=-1;
    num=0;
}
void add(int u,int v,int w)
{
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=head[u];
    head[u]=num++;
}
int dij(int x)
{
    priority_queue<pii,vector<pii>,greater<pii> >Q;
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)
    dis[i]=INF;
    dis[1]=0;
    Q.push(pii(dis[1],1));
    while(!Q.empty())
    {
        pii cur=Q.top();
        Q.pop();
        int u=cur.second;
        if(vis[u]) continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].next)
        if(dis[edge[i].v]>dis[u]+edge[i].w&&i!=x)
        {
            if(x==-1)
            {
                pedge[edge[i].v]=i;
                pnode[edge[i].v]=u;
            }
            dis[edge[i].v]=dis[u]+edge[i].w;
            Q.push(pii(dis[edge[i].v],edge[i].v));
        }
    }
    return dis[n];
}
int solve()
{
    int x=-1;
    int ans=dij(x);
    if(ans==INF)
        return -1;
    for(int i=n;i!=1;i=pnode[i])
    {
        x=pedge[i];
        ans=max(ans,dij(x));
        if(ans==INF)
        return -1;
    }
    return ans;
}
int main()
{
    int a,b,c;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        while(m--)
        {
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
        }
        printf("%d\n",solve());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值