Harry Potter and the Final Battle

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

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   |   We have carefully selected several similar problems for you:  3987 3988 3983 3984 3985 
 
 //453MS    3616K    2184 B    G++
/* 题意:
给出n个点,m条边的图,可去掉其中任意一条边,求最坏情况下 点1到点n 的最短路径 最短路径:
先一次spfa求出最短路,然后保存路径,保存路径后遍历该路径,从而求解 */
#include<iostream>
#include<vector>
#include<queue>
#define inf 0x7ffffff
#define N 1005
using namespace std;
struct node{
int v,w,id; //id记录其为第几条边
node(int a,int b,int c){
v=a;w=b;id=c;
}
};
int d[N],q[N],path[N]; //q[i]记录最短路中第i个点用到的边,path记录最短路径
bool vis[N],pre[*N]; //pre[i]将第i条边暂时隐去
int n,m;
vector<node>V[N];
bool flag; //求最短路与遍历最短路的开关
void spfa()
{
queue<int>Q;
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++) d[i]=inf;
d[]=;
vis[]=true;
Q.push();
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=false;
int n0=V[u].size();
for(int i=;i<n0;i++){
int v=V[u][i].v;
int w=V[u][i].w;
int id=V[u][i].id;
if(pre[id]) continue; //如果遍历到此边跳过
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(flag){
path[v]=u; q[v]=id;
}
if(!vis[v]){
Q.push(v);
vis[v]=true;
}
}
}
}
}
int main(void)
{
int t;
int a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
a--;b--;
V[a].push_back(node(b,c,i));
V[b].push_back(node(a,c,i));
}
flag=true;
memset(pre,false,sizeof(pre));
spfa();
flag=false;
if(d[n-]==inf){
puts("-1");continue;
}
int ans=inf;
bool tflag=true;
for(int i=n-;i!=;i=path[i]){
pre[q[i]]=true; //最短路边的开关
spfa();
pre[q[i]]=false;
if(d[n-]==inf){
ans=inf;break;
}
if(tflag){
ans=d[n-]; tflag=false;
}else ans=max(ans,d[n-]);
}
if(ans==inf) puts("-1");
else printf("%d\n",ans);
}
return ;
}

hdu 3986 Harry Potter and the Final Battle (最短路径)的更多相关文章

  1. 【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 ...

  2. hdu 3986 Harry Potter and the Final Battle

    一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA...... 思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边.所以枚举一下最短路上的 ...

  3. hdu3986Harry Potter and the Final Battle

    给你一个无向图,然后找出当中的最短路, 除去最短路中的随意一条边,看最糟糕的情况下, 新的图中,第一个点到末点的最短路长度是多少. 我的做法是: 首先找出最短路,然后记录路径, 再一条一条边的删, 删 ...

  4. hdu 3986(最短路变形好题)

    Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/6553 ...

  5. HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)

    Harry Potter and the Hide Story Problem Description iSea is tired of writing the story of Harry Pott ...

  6. HDU 3986

    http://acm.hdu.edu.cn/showproblem.php?pid=3986 从开始的最短路里依次删一条边,求新的最短路,求最长的最短路 删边操作要标记节点以及节点对应的边 #incl ...

  7. HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)

    Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/ ...

  8. hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

    view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...

  9. Bridges: The Final Battle

    对修改操作按时间分治,设$solve(l,r,n,m)$为考虑时间在$[l,r]$的修改操作,作用范围是$n$个点,$m$条边的图. 若$l=r$,则暴力Tarjan统计桥边个数即可. 否则提取出$[ ...

随机推荐

  1. 微信小程序清除默认样式

    1.清除button的默认样式 button::after{border:none;}input{outline:none;border:none;list-style: none;}

  2. scoped,会使设置UI组件库的样式识别不出来

    未设置 scoped 作用域:显示效果 设置作用域的效果:ui组件默认的值(你怎么设置都不管用)

  3. JS - 简单的下载图片至本地

    <iframe id="saveImg" src="图片路径" style="display:none;"></ifram ...

  4. python数据类型的转换

  5. Shell学习——Shell分类:登录shell和非登陆shell 交互shell和非交互shell

    1.从两个不同维度来划分,是否交互式,是否登录 2.交互式shell和非交互式shell 交互式模式:在终端上执行,shell等待你的输入,并且立即执行你提交的命令.这种模式被称作交互式是因为shel ...

  6. python__基础 : sys模块: sys.argv与sys.path

    sys模块中的 argv 保存的是当你运行一个py文件的时候给他传递进去的参数,如: import sys a = sys.argv print(a) # 当在命令行中调用这个py文件: > p ...

  7. PHP 微信公众号真正正确的客服头像上传

    首先我们来看官方文档 这TM的搞笑呢 什么破玩意儿! 需要条件 1 需要有一个客服的账号 (废话) 2 一致jpg格式的图片(扯蛋) 完整流程 1 获取access_token 2获取账号 3 $ur ...

  8. 守护进程,进程安全,IPC进程间通讯,生产者消费者模型

    1.守护进程(了解)2.进程安全(*****) 互斥锁 抢票案例3.IPC进程间通讯 manager queue(*****)4.生产者消费者模型 守护进程 指的也是一个进程,可以守护着另一个进程 一 ...

  9. 开放定址法——平方探测(Quadratic Probing)

    为了消除一次聚集,我们使用一种新的方法:平方探测法.顾名思义就是冲突函数F(i)是二次函数的探测方法.通常会选择f(i)=i2.和上次一样,把{89,18,49,58,69}插入到一个散列表中,这次用 ...

  10. 14、函数之匿名函数(lambda)

    关键字lambda可以创建匿名函数,语法是:lambda 参数s :表达式.匿名函数与普通函数只有以下几点不同:①没有函数名:②只能有一个表达式:③一定会有返回值,返回值就是该表达式的结果. 另外,匿 ...