hdu 3986 (枚举+最短路)

Harry Potter and the Final Battle

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


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
 

Recommend
lcy
 
PS:首先鄙视下自己比赛时直接想复杂了。。。懒得写堆维护用spfa,再次。。。 鄙视
分析:这题要求去掉一条边,使得最短路最长,首先确定去掉不是最短路上的边就是无用功了,所以,那条边必须是最短路上的边,于是先做一次最短路,求出最短路上的边,然后枚举去掉哪条边,再求最短路,使得最短路最长即答案,注意重边的情况,答案赋初值为第一次最短路的的答案,因此wa了一次,,,,囧
代码:
#include<cstdio>
using namespace std;
const int mm=111111;
const int mn=1111;
const int oo=1000000000;
int ver[mm],next[mm];
int head[mn],dis[mn],q[mm],p[mn],d[mn][mn][2];
bool vis[mn];
int n,edge;
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline int max(int a,int b)
{
    return a>b?a:b;
}
inline void prepare(int n)
{
    while(n)vis[n]=0,head[n--]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    if(c<d[u][v][0])
    {
        if(d[u][v][0]==oo)
        {
            d[u][v][0]=d[v][u][0]=c;
            ver[edge]=v,next[edge]=head[u],head[u]=edge++;
            ver[edge]=u,next[edge]=head[v],head[v]=edge++;
        }
        else d[v][u][1]=d[u][v][1]=d[u][v][0],d[u][v][0]=d[v][u][0]=c;
    }
    else d[v][u][1]=d[u][v][1]=min(d[u][v][1],c);
}
int spfa(bool save)
{
    int i,u,v,l,r=0,tmp;
    for(i=1;i<=n;++i)dis[i]=oo;
    dis[q[r++]=1]=0;
    if(save)p[1]=p[n]=-1;
    for(l=0;l!=r;(++l>=mm)?l=0:1)
        for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i])
            if(dis[v=ver[i]]>(tmp=dis[u]+d[u][v][0]))
            {
                dis[v]=tmp;
                if(save)p[v]=u;
                if(vis[v])continue;
                vis[q[r++]=v]=1;
                if(r>=mm)r=0;
            }
    return dis[n];
}
int main()
{
    int i,j,k,m,t,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)d[i][j][0]=d[i][j][1]=oo;
        prepare(n);
        while(m--)scanf("%d%d%d",&i,&j,&k),addedge(i,j,k);
        ans=spfa(1);
        for(i=n,j=p[n];i!=1&&j>0;i=j,j=p[j])
        {
            k=d[i][j][0];
            d[i][j][0]=d[j][i][0]=d[i][j][1];
            ans=max(ans,spfa(0));
            d[i][j][0]=d[j][i][0]=k;
            if(ans>=oo)break;
        }
        if(ans>=oo)printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值