hdu1595find the longest of the shortest 最短路
//给一个无向图,问删除一条边,使得从1到n的最短路最长
//问这个最长路
//这个删除的边必定在最短路上,假设不在。那么走这条最短路肯定比其它短
//枚举删除这条最短路的边,找其最长的即为答案
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 1110 ;
const int inf = 0x3f3f3f3f;
struct Edge
{
int u , v , w ;
int next ;
}edge[maxn*maxn] ;
int head[maxn] ;
int nedge ;
int F[maxn] ;
int vis[maxn] ;
int dis[maxn] ;
int a[maxn] ;
int n , m;
void addedge(int u , int v , int w)
{
edge[nedge].u = u ;
edge[nedge].v = v ;
edge[nedge].w = w ;
edge[nedge].next = head[u] ;
head[u] = nedge++ ;
}
void dijkstra(int num)
{
memset(vis , 0 , sizeof(vis)) ;
for(int j = 2;j <= n;j++)
dis[j] = inf ;
dis[1] = 0 ;
while(1)
{
int mi = inf; int pos ;
for(int j = 1 ;j <= n;j++)
if(!vis[j] && dis[j] < mi)
mi = dis[pos = j] ;
if(mi == inf)break;
vis[pos] = 1 ;
for(int i = head[pos] ;i != -1 ;i = edge[i].next)
{
if(i == num || i == (num^1))
continue ;
int v = edge[i].v;
if(dis[v] > dis[pos] + edge[i].w)
{
dis[v] = dis[pos] + edge[i].w ;
F[v] = i ;
}
}
}
}
int main()
{
//freopen("in.txt" ,"r" , stdin) ;
while(~scanf("%d%d" , &n , &m))
{
memset(head , -1 , sizeof(head)) ;
nedge = 0 ;
memset(F , 0 , sizeof(F)) ;
while(m--)
{
int u , v , w ;
scanf("%d%d%d" ,&u , &v ,&w) ;
addedge(u , v , w) ;
addedge(v , u , w) ;
}
dijkstra(nedge) ;
int u = n ;
int ans = 0 ;
int len = 0 ;
while(1)
{
if(u == 1)break ;
a[++len] = F[u] ;
u = edge[F[u]].u ;
}
for(int i = 1;i <= len;i++)
{
dijkstra(a[i]) ;
ans = max(ans , dis[n]) ;
}
cout<<ans<<endl;
}
return 0 ;
}
hdu1595find the longest of the shortest 最短路的更多相关文章
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu1595 find the longest of the shortest(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limit: 100 ...
- find the longest of the shortest (hdu 1595 SPFA+枚举)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU-1595Find the longest of shortest(最短路径的最长路Dijkstra+记录路径)
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she do ...
- HDU 1595 find the longest of the shortest【次短路】
转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- hdu 1595 find the longest of the shortest(dijkstra)
Problem Description Marica is very angry with Mirko because he found a new girlfriend and she seeks ...
- HDU1595-find the longest of the shortest-dijkstra+记录路径
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she do ...
- SGU 185 Two shortest 最短路+最大流
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068 Yesterday Vasya and Petya qua ...
随机推荐
- Context的正确使用
一.Context的作用 Context的最大作用就是我们可以通过传递它来获得其他Activity或Application的相关资源和方法,它就相当于它们的引用,我们通过引用来获得对象的封装,这也是我 ...
- CSS 样式的优先级(重要,一定要理解)
1. 同一元素引用了多个样式时,排在后面的样式属性的优先级高 例如,下面的 div,同时引用了 [.default] 和 [.user] 中的样式,其中 [.user] 样式中的 width 属性会替 ...
- 【Spring】AOP
AOP 编程允许你把遍布应用各处的功能分离出来形成可重用的组件,将安全.事务和日志关注点与你的核心业务逻辑相分离. 面向切面编程往往被定义为促使应用程序分离关注点的一项技术.系统由许多不同组件组成,每 ...
- canvas杂记
canvas杂记 canvas标签 <canvas width=600 height=400></canvas> //1. 拿到画布 var canvas = document ...
- JS高级——函数的调用模式
函数调用模式一共有四种 <script> //1.函数模式 //this指向window全局对象 //2.方法模式 //this指向调用这个方法的对象 //3.构造函数模式 //this ...
- C#——数据库的访问
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Zabbix 监控redis
Zabbix 监控redis 1.监控脚本,github上的 [root@localhost ~]# cat /etc/zabbix/script/redis-status.sh #!/bin/bas ...
- scp: /xxxx: not a regular file
问题描述 scp root@10.2.1.92:/home /home/wangju/databakroot@10.2.1.92's password: xxxxscp: /home: not a r ...
- chrome浏览器 快捷键设置
如何设置谷歌浏览器在新窗口中打开链接?如何设置谷歌浏览器在新标签页中打开链接?一.快捷键方式: 1.左键单击 ==> 在当前窗口中打开目标网页. 2.Shift + 左键单击 ==> 在新 ...
- sublime text3 verilog代码编写高级操作篇
2018.10.21 好久没写博客了,这段时间一直在学习一直在沉淀,然而发现学的越多会的更少,只能快马加鞭吧! 博主从大一暑假接触FPGA,到现在快一年半了,时间恍逝.刚开始入门也是用的quartus ...