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

[email protected]

Source

2011 Multi-University Training Contest 15 - Host by WHU

解题:先用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);
    }
}
时间: 2024-08-08 06:06:30

HDU3986Harry Potter and the Final Battle(SPFA册边)的相关文章

hdu3986Harry Potter and the Final Battle

给你一个无向图,然后找出其中的最短路, 除去最短路中的任意一条边,看最糟糕的情况下, 新的图中,第一个点到末点的最短路长度是多少. 我的做法是: 首先找出最短路,然后记录路径, 再一条一条边的删, 删一条算一下最短路长度, 之后恢复这条边,删掉下一条边继续算, 以此类推. 看之中最糟糕的情况下,最短路长度是多少, 如果是无穷大则代表最坏情况为不通,按题意输出-1即可, 否则输出最坏情况下,最短路长度. 我用spfa和链式向前星做的, 代码如下: #include<iostream> #incl

hdu 3986 Harry Potter and the Final Battle spfa变形

#include<stdio.h> #include<string.h> #include<queue> #include<vector> using namespace std; const int N=1024; const int inf=0x7fffffff; struct Edge { int u,v,w,use,del; }; vector<Edge>edge; vector<int>G[N]; int n,m,dist[

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): 3319    Accepted Submission(s): 936 Problem Description The final battle is coming. Now Harry Potter is located

【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle

http://acm.hdu.edu.cn/showproblem.php?pid=3986 [题意] 给定一个有重边的无向图,T=20,n<=1000,m<=5000 删去一条边,使得1~n的最短路最长 求最短路最长是多少 [思路] 一定是删最短路上的边 可以先跑一个Dijkstra,求出最短路,然n后枚举删边 朴素的Dijkstra为n^2,枚举删边(n条)需要的总时间复杂度是n^3 堆优化Dijkstra(nlogn),总复杂度为n^2logn 有多重边,用邻接矩阵不方便,用邻接表方便,

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123