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

【BUAA】【计组】计算机组成 pre (含详细解析) BUAA 计算机组成 本文章旨在为@走肖暂时无法接通的系列文章创造一个目录,收藏本文章,方便查阅! 1. 入门测试 2. 基础知识 3. Logisim   欢迎来到 Logisim 的世界   Logisim 门电路   Logisim 组合电路   Logisim 时序电路   Logisim 仿真与调试   应用与挑战 4. Verilog 与仿真工具   前言   ISE 入门   VCS 入门   Verilog 语法   Verilog 题目实例与分析   Verilog 工程的设计开发调试 阅读详情

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 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
题意:给出一个无向图,删除图中的任意一条边,使得从结点1到结点n的最短路径最长。
思路:显然删除的边一定是在结点1到结点n未删边时的最短路径上,我们可以保存未删边时结点1到结点n的最短路径,然后枚举删除最短路径上的每一条边,再求一次最短路径,最后取最大值。当某一次求最短路径时结点1无法到达结点n,则输出-1.
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <cstdlib>
using namespace std;

const int INF=1000000000;
const int maxn=1005;
const int maxm=100005;
struct node
{
    int v,w;
    int next;
}edge[maxm];
typedef pair<int,int> pii;
int head[maxn],dis[maxn],pedge[maxm],pnode[maxn];
bool vis[maxn];
int n,m,num;
void init()
{
    for(int i=1;i<=n;i++)
    head[i]=-1;
    num=0;
}
void add(int u,int v,int w)
{
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=head[u];
    head[u]=num++;
}
int dij(int x)
{
    priority_queue<pii,vector<pii>,greater<pii> >Q;
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)
    dis[i]=INF;
    dis[1]=0;
    Q.push(pii(dis[1],1));
    while(!Q.empty())
    {
        pii cur=Q.top();
        Q.pop();
        int u=cur.second;
        if(vis[u]) continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].next)
        if(dis[edge[i].v]>dis[u]+edge[i].w&&i!=x)
        {
            if(x==-1)
            {
                pedge[edge[i].v]=i;
                pnode[edge[i].v]=u;
            }
            dis[edge[i].v]=dis[u]+edge[i].w;
            Q.push(pii(dis[edge[i].v],edge[i].v));
        }
    }
    return dis[n];
}
int solve()
{
    int x=-1;
    int ans=dij(x);
    if(ans==INF)
        return -1;
    for(int i=n;i!=1;i=pnode[i])
    {
        x=pedge[i];
        ans=max(ans,dij(x));
        if(ans==INF)
        return -1;
    }
    return ans;
}
int main()
{
    int a,b,c;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        while(m--)
        {
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
        }
        printf("%d\n",solve());
    }
    return 0;
}


到底是什么决定了二极管的最高工作频率? 硬件工程师,应该都用过二极管的吧,不过也许有一个误区,大多数人可能并不知道,或者说是理解有问题,下面就来细细说下。 一个问题 先提一个问题:到底是什么决定了二极管的最高工作频率? 估计有不少人会回答是二极管反向恢复时间Trr,也有人会说是二极管结电容,那到底谁是对的呢?或者说都一样,反向恢复时间由结电容决定? 到底什么决定了二极管的最高工作频率,我们暂且不论,不过需要知道的是,二极管的反向恢复时间和结电容根本就是两回事,反向恢复时间绝不能等同于结电容的充放电时间。 为什么我这么说呢?. 阅读详情

相关推荐

gm/ID设计方法实践指南:从理论曲线到电路优化的关键步骤

本文详细介绍了gm/ID设计方法在模拟IC设计中的实践指南。该方法通过直接利用工艺模型数据,将设计焦点从盲目调整晶体管尺寸转向选择关键的gm/ID工作点,从而在先进工艺下实现功耗、增益和带宽等性能的精准权衡与优化。文章从理论曲线生成到具体电路设计步骤,提供了完整的实战流程。

pandas7gardener的博客 484

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

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

嗯。 1303

EM4095低频RFID曼彻斯特解码实战:从原理图到MCU代码实现

本文详细介绍了EM4095低频RFID读卡器的设计与实现,从芯片工作原理、天线设计到曼彻斯特解码算法。重点讲解了基于STM32的MCU代码实现,包括定时器捕获、数字滤波和错误处理,为开发125kHz RFID系统提供实用指南。

oil88的博客 726

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

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

chen_minghui的博客 459

WSL2+Docker+Ollama:解锁Windows本地GPU加速运行DeepSeek-R1模型

本文详细介绍了在Windows系统上,通过WSL2、Docker和Ollama的组合方案,实现本地GPU加速运行DeepSeek-R1模型的全过程。该方案解决了Windows环境下Ollama默认无法调用NVIDIA显卡的痛点,通过配置Docker的GPU支持,强制将计算任务分配给GPU,从而显著提升文本生成速度与效率,为开发者提供了一个便捷的本地AI测试环境。

weixin_28452161的博客 358

使用Typora时LaTeX内联公式简单汇总

0、前言 在使用Typora写笔记时,有时候需要添加上LaTeX排版的公式。以下是整理出的一份简单笔记,方便记忆不清楚时查阅。笔记暂时不够完善,之后再补充。 注:有些公式语法KateX不能解析,但在Typora中可以使用(不用在意) 1、一些基本内联公式 位置关系 展示 LaTeX 说明 x2x^{2}x2 x^{2} 右上标 x2x_{2}x2​ x_{2} 右下标 2x{}^{2}x2x {}^{2}x 左上标 2x{}_{2}x2​x {}_{2}x 左下标

白白旧维的博客 4451

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

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

围巾的ACM博客 503

B站自动答题协议分析

使用Python写了一个自动答题机器人!基于接口协议实现且包含点选验证码识别

吴秋霖的博客 2666

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

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

Shoutmon的专栏 752

33、密码学中的选择密文攻击与Cramer - Shoup加密方案

本文详细介绍了密码学中选择密文攻击的背景及其对Cramer-Shoup公钥加密方案的影响。该方案在决策Diffie-Hellman假设和抗碰撞哈希函数的基础上,能够有效抵御自适应选择密文攻击,并且其安全性无需依赖随机预言机模型。文章还分析了Cramer-Shoup方案的设计原理、加密与解密过程,以及其在标准假设下的安全性证明方法。此外,通过一系列相关练习,帮助读者进一步理解密码学概念如密文不可区分性、ElGamal加密、Goldwasser-Micali方案等。最后,文章展望了密码学的未来发展趋势,包括量子

vscode6remote的博客 101

高通MBIM拨号流程简述

高通平台MBIM流程简述

不秃头的程序猿Gabriel的博客 5073

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,取中途...

小衣同学的博客 364

基于FFT+BP神经网络/决策树的轴承故障诊断

基于FFT+BP神经网络/决策树的轴承故障诊断

天天酷科研的博客 138

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

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

晚风中的自由 649

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

怀旧版电脑GBC模拟器

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

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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

hdu 3986 Harry Potter and the Final Battle

很多人认为这是在求次短路,其实不然,这个题只要spfa()一次,先把最短路径求出来,在枚举拆掉最短路的每一条边,每次拆一条边就进行一次spfa(),之后还原先前拆的边,该拆另一条边,记录最小的时间即可。 #include #include #define INF 1000000 #define MAXN 2005 using namespace std; struct

South__wind的专栏 677

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

HDU3986 Harry Potter and the Final Battle

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 ch...

qq_36552779的博客 315

#HDU 3986 Harry Potter and the Final Battle (枚举 + 最短路 + 保存点和边操作)

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 wil...

OlaMins 297

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

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

poursoul 677

HDU 3986 Harry Potter and the Final Battle(Dijkstra)

HDU 3986 Harry Potter and the Final Battle(Dijkstra) http://acm.hdu.edu.cn/showproblem.php?pid=3986 题意:给你一个无向图,现在在删除图中一条边的情况下,要你求从1号点到N号点的最短距离的最大值. 分析:        明显删除的这条边一定要在1到N的最短路径上才是对结果有影响的.所以我们只需

code 1414

hdu3986 Harry Potter and the Final Battle(spfa)

题目链接 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 wil...

qq_42479630的博客 171

HDU3986Harry 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): 2666    Accepted Submission(s): 761 Problem Descriptio

青山绿水之辈 专栏 1130

HDU 3986 Harry Potter and the Final Battle

/* 题意:在一张图上删去一条边,使得从1到n的最短路最长 刚开始枚举要删去的边x,求最短路,复杂度是m*n*log(n);果断tle了 后来想想只要在不删边的情况下求得最短路,记录最短路经过的边,然后枚举这些边中的一条为删去的边求最短路,使得这个值最多即可 很显然,删去的边不是

Join Hands 1208

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

hdu3986 Harry Potter and the Final Battle

题意:给出一张图,删去其中任意一条边,使得从1到n的最短路径最长 思路很简单,只要先求出1到n的最短路,然后枚举删去最短路上的边再求一次最短路,取其中最长的就行了 #include #include #include #include using namespace std; const int INF=10000000; const int maxn=1111; const

Clearlemon 760
上一篇: hdu 1534 Schedule Problem(差分约束求最长路)
下一篇: hdu 4034 Graph(floyd)
KinFungYu
博客等级 码龄14年 39粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值