HDU3986Harry Potter and the Final Battle(SPFA删边)

Harry Potter and the Final Battle

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


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
 

Author
tender@WHU
 

Source
解题:先用spfa求一条最短路,并记下该边的标记。接下来就是枚举一条一条边(记下的边)删,每次只能删一条边,当遇到一条必须边时就可以直接输出 -1,因为从1不可到达n。如果总是有一条路,那么就输出最大的那条路的花费。
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1005;
const int inf = 999999999;
struct EDG
{
    int v,d,id;
};
vector<EDG>mapt[N];
bool inq[N];
int dis[N],frome[N],id[N],n;
void init()
{
    for(int i=0;i<=n;i++)
    {
        mapt[i].clear(); id[i]=inf; frome[i]=i;
    }
}
inline void spfa(int a,bool recode)
{
    queue<int>q;
    int s;
    for(int i=1;i<=n;i++)
        dis[i]=inf,inq[i]=false;
    inq[n]=true;
    dis[1]=0; q.push(1);
    while(!q.empty())
    {
        s=q.front(); q.pop();
        inq[s]=false;
        for(int i=0;i<mapt[s].size();i++)
        if(id[a]!=mapt[s][i].id||recode)
        {
            int now=mapt[s][i].v;
            if(dis[now]>dis[s]+mapt[s][i].d)
            {
                dis[now]=dis[s]+mapt[s][i].d;
                if(recode)
                    frome[now]=s,id[now]=mapt[s][i].id;
                if(!inq[now])
                    inq[now]=true,q.push(now);
            }
        }
    }
}
int main()
{
    int m,a,b,ans,t;
    EDG edg;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&edg.d);
            edg.id=m;
            edg.v=b; mapt[a].push_back(edg);
            edg.v=a; mapt[b].push_back(edg);
        }
        if(n==1)
        {
            printf("0\n");continue;
        }
        spfa(0,true);
        ans=-1;
        a=n;
        while(frome[a]!=a)
        {
            b=frome[a];
            spfa(a,false);
            if(dis[n]>ans&&dis[n]!=inf)
                ans=dis[n];
            else if(dis[n]==inf)
            {
                ans=-1; break;
            }
            a=b;
        }
        printf("%d\n",ans);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值