博客
关于我
hdu3986 Harry Potter and the Final Battle(spfa)
阅读量:396 次
发布时间:2019-03-05

本文共 3102 字,大约阅读时间需要 10 分钟。


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
2011 Multi-University Training Contest 15 - Host by WHU
思路:由于数据范围还行,考虑枚举最短路上的每条边,把这条边删除后跑一遍spfa。

#include<bits/stdc++.h>using namespace std;const int maxn = 5e4+5;const int inf=1e9+100;int d[maxn];struct cxk{       int v,w,id;};int flag=1,p[maxn],pre[maxn],num[maxn];vector<cxk>g[maxn];bool vis[maxn];void spfa(int x){       memset(vis,false,sizeof(vis));    for(int i=0;i<maxn;++i) d[i]=inf;    d[x]=0;    queue<int>q;    q.push(x);    vis[x]=true;    while(!q.empty())    {           int top=q.front();        q.pop();        vis[top]=false;        for(auto v:g[top])        {               if(p[v.id]) continue;            if(d[top]+v.w<d[v.v])            {                   d[v.v]=d[top]+v.w;                if(flag)                {                       pre[v.v]=top;num[v.v]=v.id;                }                if(!vis[v.v]) q.push(v.v),vis[v.v]=true;            }        }    }}int main(){       int T,n,u,v,w,m;    scanf("%d",&T);    while(T--)    {           memset(p,0,sizeof(p));        memset(pre,0,sizeof(pre));        memset(num,0,sizeof(num));        flag=1;        scanf("%d %d",&n,&m);        for(int i=0;i<=n;++i) g[i].clear();         for(int i=1;i<=m;++i)        {               scanf("%d %d %d",&u,&v,&w);            u--,v--;            g[u].push_back({   v,w,i});            g[v].push_back({   u,w,i});        }        spfa(0);        if(d[n-1]==inf) {               printf("-1\n");continue;        }        flag=0;        int ans=0,flag1=0;        for(int i=n-1;i!=0;i=pre[i])        {               p[num[i]]=1;            spfa(0);            p[num[i]]=0;            if(d[n-1]==inf) {               flag1=1;printf("-1\n");break;        }        ans=max(ans,d[n-1]);        }        if(flag1) continue;        if(ans==inf) puts("-1");        else printf("%d\n",ans);    }}

转载地址:http://hrewz.baihongyu.com/

你可能感兴趣的文章
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>