码迷,mamicode.com
首页 > 其他好文 > 详细

HDU3986Harry Potter and the Final Battle(SPFA册边)

时间:2015-04-12 21:08:26      阅读:193      评论:0      收藏:0      [点我收藏+]

标签: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);
    }
}


HDU3986Harry Potter and the Final Battle(SPFA册边)

标签:spfa   图论   

原文地址:http://blog.csdn.net/u010372095/article/details/45013717

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!