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): 2118 Accepted Submission(s): 580
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.
4
2
2
-1
2
//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 (最短路径)的更多相关文章
- 【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 ...
- hdu 3986 Harry Potter and the Final Battle
一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA...... 思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边.所以枚举一下最短路上的 ...
- hdu3986Harry Potter and the Final Battle
给你一个无向图,然后找出当中的最短路, 除去最短路中的随意一条边,看最糟糕的情况下, 新的图中,第一个点到末点的最短路长度是多少. 我的做法是: 首先找出最短路,然后记录路径, 再一条一条边的删, 删 ...
- hdu 3986(最短路变形好题)
Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/6553 ...
- 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 ...
- HDU 3986
http://acm.hdu.edu.cn/showproblem.php?pid=3986 从开始的最短路里依次删一条边,求新的最短路,求最长的最短路 删边操作要标记节点以及节点对应的边 #incl ...
- HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)
Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/ ...
- hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...
- Bridges: The Final Battle
对修改操作按时间分治,设$solve(l,r,n,m)$为考虑时间在$[l,r]$的修改操作,作用范围是$n$个点,$m$条边的图. 若$l=r$,则暴力Tarjan统计桥边个数即可. 否则提取出$[ ...
随机推荐
- 大白话讲解BP算法(转载)
最近在看深度学习的东西,一开始看的吴恩达的UFLDL教程,有中文版就直接看了,后来发现有些地方总是不是很明确,又去看英文版,然后又找了些资料看,才发现,中文版的译者在翻译的时候会对省略的公式推导过程进 ...
- Swift项目,适配遇到的问题
Swift4.x 控制器自带xib加载在iOS8系统崩溃 // MARK: - 解决控制器自带xib加载在iOS8系统崩溃的问题.iOS8.x,需要给控制器的xib重写一下init 方法 overri ...
- jQuery选择器与事件学习笔记
层次选择器: $("div li")获取div下的所有li元素(后代.子.子的子......) $("div>li")获取div下的直接li子元素. ...
- Lucene检索提高性能的几个方式
1.采用最新版本的Lucene 2.索引文件存储采用本地文件系统,如果需要挂载远程系统,请采用 readonly方式. 3.当然采用更好的硬件,更高I/O的磁盘 4.提高OS 缓存,调整参数 5.提高 ...
- esdoc 自动生成接口文档介绍
原文地址:https://www.xingkongbj.com/blog/esdoc/creat-esdoc.html 官网 ESDoc:https://esdoc.org/ JSDoc:http:/ ...
- 本地预览的vue项目,在githubpage静态展示
本地项目github静态展示 前提 在本地npm run dev后能够在本地端口正常显示 githubpage为自己的静态页面 上线 config/index.js中设置assetsPublicPat ...
- 牛客小白月赛1 I あなたの蛙が帰っています 【卡特兰数】
链接:https://www.nowcoder.com/acm/contest/85/I题目描述 あなたの蛙が帰っています! 蛙蛙完成了一趟旅行,回家啦!但它还是没有去它心中非常想去的几个地方.总共 ...
- 概述「DAG加边至强连通」模型&&luoguP2746校园网Network of Schools
模型概述 有一DAG,问最少加多少条边能够使图强连通. 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的 ...
- Centos7上docker的安装和基本使用
Centos7上Docker的相关操作 Docker的安装 1.检查内核版本 docker使用linux的内核必须是3.10以上,首先需要 # uname -r 2.使用yum安装docker # y ...
- json loads/dumps
json.dumps : dict转成str json.loads:str转成dict dict_ = {1: 2, 3: 4, "} print(type(dict_), dict_) # ...