HDU3986 Harry Potter and the Final Battle最短路

C++学习(四六六)Multiple parse contexts are available for this file Note: Multiple parse contexts are available for this file. Choose the preferred one from the editor menu 参考: "Multiple parse contexts" in Qt creator | Qt ForumHi Do you use sessions? https://bugreports.qt.io/browse/QTCREATORBUG-18521 Anyway, its not. 阅读详情
 

Harry Potter and the Final Battle

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


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
 
先求一次最短路,枚举删除最短路上的边再求最短路,取最大值即为答案。
这么简单的一道题竟然TLE+WA无数次。。。
我的错误:
1.第一次用dij而不是用spfa,估计是导致TLE的原因
2.答案取最大值就不能用-1来更新答案,应该用inf更新
 
代码:
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 999999999
using namespace std;

int t,n,m,flag,num,adj[1005],low[1005],pre[1005],q[1005];
struct edge
{
	int u,v,w,f,pre;
}e[200005];
void insert(int u,int v,int w)
{
	e[num].u=u;
	e[num].v=v;
	e[num].w=w;
	e[num].f=1;
	e[num].pre=adj[u];
	adj[u]=num++;
}
int spfa()
{
	int i,x,v,w,f[1005]={0},head=0,tail=0;
	for(i=1;i<=n;i++)
		low[i]=inf;
	low[1]=0;
	q[++tail]=1;
	while(head!=tail)
	{
		x=q[head=(head+1)%1005];
		f[x]=0;
		for(i=adj[x];~i;i=e[i].pre)
			if(e[i].f&&low[v=e[i].v]>(w=low[x]+e[i].w))
			{
				low[v]=w;
				if(flag)
					pre[v]=i;
				if(!f[v])
				{					
					f[v]=1;
					q[tail=(tail+1)%1005]=v;
				}
			}
	}
	return low[n];
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		int i,j,k,u,v,w,ans=-1;
		num=0;
		memset(adj,-1,sizeof(adj));
		scanf("%d%d",&n,&m);
		while(m--)
		{
			scanf("%d%d%d",&u,&v,&w);
			insert(u,v,w);
			insert(v,u,w);
		}
		memset(pre,-1,sizeof(pre));
		flag=1;
		spfa();
		flag=0;
		i=n;
		while(pre[i]!=-1)
		{
			j=pre[i];
			e[j].f=e[j^1].f=0;
			k=spfa();
			e[j].f=e[j^1].f=1;
			if(k>ans)
				ans=k;
			i=e[j].u;
		}
		if(ans<inf)
			printf("%d\n",ans);
		else
			puts("-1");
	}
}

088、多摄像头跨镜目标追踪 ID 频繁切换?ReID 特征 + 轨迹关联 + 时间约束方案 文章摘要:本文针对多摄像头跨镜目标追踪中ID频繁切换的问题,提出了一种结合ReID特征、轨迹关联和时间约束的综合解决方案。作者分析了ID切换的三个主要原因:ReID特征不稳定、轨迹关联依赖最近邻匹配以及忽略时间约束。通过改进ReID特征提取(多层特征融合)、引入轨迹预测(卡尔曼滤波)和添加时间窗口约束,成功将ID切换率从35%降至3%以下。文章详细分享了实现细节和踩坑经验,为跨摄像头目标追踪提供了实用优化思路。 阅读详情

相关推荐

扩展卡尔曼滤波(EKF)理论讲解与实例(matlab、python和C++代码)

扩展卡尔曼滤波(EKF)理论讲解与实例(matlab、python和C++代码) 文章目录扩展卡尔曼滤波(EKF)理论讲解与实例(matlab、python和C++代码)理论讲解KF和EKF模型对比雅可比矩阵计算计算实例应用实例线性模型CV模型:CA模型非线性模型CTRV模型:CTRV实例(python)small demo抛物线demo飞机高度demoC++实例参考文献 我们上篇提到的卡尔曼滤波是用于线性系统,预测(运动)模型和观测模型是在假设高斯和线性情况下进行的。简单的卡尔曼滤波必须应用在符合高斯分布

O_MMMM_O的博客 10万+

hdu3986Harry Potter and the Final Battle(最短路+枚举)

题意:给一个无向图,问任意删除一条边,求点1到点n的最短路径的最长长度 题解:显然删除的边只能是最短路径上的边,所以只需要记录最短路径上的边,然后枚举删除每一条,求最大值即可 #include #include #include #include #include #include #include #include #include #include #inclu

围巾的ACM博客 503

error C2039: “int_least8_t”: 不是“`global namespace'”的成员

在向H.265的测试软件HM16.0中配置boost的时候,发现 include &lt;boost/cstdint.hpp&gt;的时候总是会报错:错误 11 error C2039: “int_fast16_t”: 不是“`global namespace'”的成员 D:\Program Files\Caffe\VSContact\boost_1_58_0\boost\cstdint.hpp ...

flyerene的博客 1万+

I - Harry Potter and the Final Battle HDU - 3986 最短路加重边,dijkstra的邻接表实现

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 roa

Cworld2017的博客 479

NVIDIA驱动装好了,Gazebo为啥还是卡?手把手教你用optirun搞定GPU加速

本文详细解析了NVIDIA驱动安装后Gazebo仍卡顿的原因,并提供三步精准诊断与GPU加速实战方案。通过optirun快速启用GPU加速,解决OpenGL与显示服务器的配置问题,显著提升仿真性能。适用于ROS、PX4等开发环境,帮助用户充分利用显卡性能。

weixin_30379531的博客 467

LoRa无线通信技术介绍(六)SX127X芯片收发程序讲解

接收LORA芯片SX127X的收发程序。

DT程序员的博客 1756

HDU3986 - Harry Potter and the Final Battle - 拆边后最短路

1.题目描述: Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3535    Accepted Submission(s): 1009 Pr

终有绿洲摇曳在沙漠 486

搭建AutoSAR开发环境

任何开发都需要一套开发环境,包括软硬件环境,笔者后续将介绍的是 NXP 的 S32K144 配套的开发板。接下来详细介绍怎么获取这些软硬件工具。仅供个人学习使用!

cfl927096306的博客 6123

hdu 3986 Harry Potter and the Final Battle 最短路spfa+枚举

题目链接 题意:给出n个点,m条边,且有重边。求除去任意一条边的最长最短路。 由于有重边的情况,所以用邻接表来存储。注意标记毁坏的边时,正反双向都要标记。 #include #include #include #include #include #define N 1100 #define INF 0x7ffffff #define M 110000 using namespace st

弱菜zc 888

Halcon 形态学 开发案例 (开运算opening(先腐蚀再膨胀)、闭运算closing(先膨胀再腐蚀))

算子方法:功能:使用用户自定义的结构元素对区域进行开运算参数: Region(输入参数) :输入区域 StructElement(输入参数) :结构元素(区域) RegionOpening(输出参数) :开运算后的区域功能:使用圆形结构元素对区域进行开运算参数: Region(输入参数) :输入区域 RegionOpening(输出参数) :开运算后的区域 Radius(输入参数) :圆形结构元素的半径功能:使用矩形结构元素对区域进行开运算。

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 1623

机器学习项目实战案例数据集

本专栏整理了《机器学习项目实战案例》,内包含了各种不同的机器学习项目,包含项目原理以及源码,每一个项目实例都附带有完整的代码+数据集。

CSDN 精品推荐 3647

HDU 3986 Harry Potter and the Final Battle 最短路

题目大意: 给你一个无向图, 蓝厚让你删掉一条边, 让剩下的图中的最短路 最长, maxmin系列。。 一开始想不出来解法, 就准备暴力一发试试, 也就是找到最短路之后一条条拆边, 然后不出意外的T了, 以为有什么厉害的算法, 结果赛后发现- -只因为删边的时候没加 if( ans == inf) break; 这一句, 啊啊啊居然卡这种数据我也是醉了, 然后就噩梦#include "iost

zz_Black的博客 469

芒果YOLOv8改进165:损失函数angleLoss:即插即用 | 独家首发原创新颖|公开数据集DOTA高效无损涨点0.5% | 旋转检测

YOLOv8改进:旋转检测Loss损失函数angleLoss:即插即用 | 独家首发原创新颖|公开数据集DOTA高效无损涨点0.5%

包括YOLOv5、YOLOv7、YOLOv8等模型改进 1157

【HDU】3986 Harry Potter and the Final Battle 最短路

传送门:【HDU】3986 Harry Potter and the Final Battle

poursoul 677

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

hdu 3986 Harry 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): 3278    Accepted Submission(s): 915 Problem Descriptio

mengxiang000000 484

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

hdu3986 Harry Potter and the Final Battle(删边最短路/枚举+dijkstra)

题目 T(T&lt;=20)组数据,n(n&lt;=1e3)个点,m(m&lt;=5e4)条边, 每条边都是双向的,从点1到点n的最短路中, 对方会炸毁一条,问你在最坏情况下最短路是多长 题解 先跑一遍dijkstra,用pre[v]和E[v]把前驱的点和边都记录下来 如果不通,就不用枚举删哪条边了 从点n向点1回溯,在回溯的路上枚举删边, 每删一条就跑一次dijkstra,取中途...

小衣同学的博客 363

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

HDU 3986 Harry Potter and the Final Battle 删掉任意一条边的最长最短路

题目来源:HDU 3986 Harry Potter and the Final Battle 题意:哈利波特要从1到n 不过敌人可以破坏一条边 求最坏的情况下到达n需要的最短时间 不能到达输出-1 思路:和上一题差不多 每次枚举最短路上的边 然后去掉该条边在做最短路 因为是无向的 不要忘记把它的反向边也删除  上一题一定有解 这题不一定 如果去掉一条边后无法到达n那么输出-1 因为敌人足够

嗯。 1303

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): 3267    Accepted Submission(s): 913 Problem Descript

TUT我好菜啊 549

HDU 3986 Harry Potter and the Final Battle 删除一条边的最长最短路

转载请注明本文地址 这题跟这题的题意是一样的,但是也有两点区别:1.1号点到n号点可以不连通 2.有重边。其中第二点可以导致题目变得更麻烦一点。 思路: 用邻接表写会简单一些,但是我还是用临界矩阵写了。 1.整体思路跟上面给的那个链接的做法是一样的,也是枚举最短路上的边(正面见那个链接),然后删除,跑最短路,取最长的那个。 2.考虑到每次只删一条边,那么即使有重边,那实际

Shoutmon的专栏 752

HDU 3986 Harry Potter and the Final Battle 删掉任意一条边的最长最短路(有重边)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3986题意哈利波特为了能够尽快世界和平,决定去攻打伏地魔,从他所在的位置,哈利波特走到伏地魔所在位置总是会选择最短的路走。伏地魔魔力强大,他会选择一条路破坏掉,输出破坏掉一条路后,最大的最短路,如果破坏前后导致哈利波特不能到达伏地魔那儿输出-1思路在样例中看到有重边,就不能像HDU1595那样用邻接矩阵存了

chen_minghui的博客 459

HDU - 3986 Harry Potter and the Final Battle(最短路Dij)

Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 4268    Accepted Submission(s): 1290Problem Description The...

GYH0730的博客 345

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): 1741    Accepted Submission(s): 487 Problem Descript

WEYuLi的专栏 1004

HDU - 3986 Harry Potter and the Final Battle(最短路SPFA+枚举删边)

题意:这一道题目和 HDU - 1595 find the longest of the shortest(最短路Dijkstra+枚举删边) 很像。不过这一道的重边要保存起来。并且在枚举边时,若遇到不可达情况,直接 -1 即可。思路:由于有重边,且重边要保存,故不可以用邻接矩阵保存。用静态链接表,也就是链式前向星。先SPFA求出最短路,保存每个点的前驱点。另外用一个数组保存每条边在 edge 数组中的序号,便于删除。然后枚举删除最短路上的边,每次删除一条边,就求一次最短路,维护 dist[n] 的最大值

晚风中的自由 649

HDU 3986 Harry Potter and the Final Battle(枚举+最短路+注意重边处理)

题目连接 题目大意:给一个n个点m条边的图,求在删掉一条边的情况下从1到n最短路的最大值。 分析:同hdu1595,但与它不同的是这个题是由重边的,所以要注意对重边的处理,这里用链式前向星存图,假设删掉的边为num,那么num^1就是其反向边,每次枚举这两条边不可使用就好了。 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<queue>

amazingee的博客 188

HDU ACM 3986 Harry Potter and the Final Battle(邻接表实现最短路dijkstra堆优化记录路径 + 枚举最短路上每条边)...

http://acm.hdu.edu.cn/showproblem.php?pid=3986 题意:   从起点1 到 终点n,删除图中任意一条边求最短路的最坏情况。   n --表示有n个点   m --边数   f t l --f和t之间有一条长度为l的道路   <<<<<<<<<<<<本题有重边>&g...

weixin_30788731的博客 99

HDU 3986 Harry Potter and the Final Battle

传送门 这道题和1595基本一样,但是有两个根本不同: 这道题明确说了有重边 这道题说不一定连通 对于第一点,用邻接表就好了,在最短路算法中记录前驱,注意,记录的是前驱的边的索引,然后回溯的时候每次删一条边,体会这个循环的写法 for (int x = N; x != 1; x = ve[pre[x] ^ 1].n)。另外,删边其实还是把边权置为INF,然后最短路算法里要加上判断当前边权不等于...

Crossing over 255
上一篇: HDU3987 Harry Potter and the Forbidden Forest最小割最少边Dinic
下一篇: HDU3996 Gold Mine最大权闭合图 2011 Multi-University Training Contest 16 - Host by TJU
power721
博客等级 码龄19年 176粉丝 135原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值