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 ...
随机推荐
- BZOJ 2506 分块
//By SiriusRen #include <bits/stdc++.h> using namespace std; ; ][],g[N],tmp=; struct Node{int ...
- JS中的面相对象
1.使用Object或对象字面量创建对象 JS中最基本创建对象的方式: var student = new Object(); student.name = "easy"; stu ...
- ACM_小凯的排序(字符串)
小凯的排序 Time Limit: 2000/1000ms (Java/Others) Problem Description: 调皮的小凯喜欢排序,拿到什么东西都要排一下序.现在他觉得单一的递增递减 ...
- Java常用类库(二):Iterator迭代器和子范围视图
今天介绍集合类的以下内容: l Iterator迭代器 l 子范围视图简介 Iterator迭代器: 应该将java迭代器认为是位于两个元素之间, 当调用next 时,迭代器就越过下一个元素,并返 ...
- java中的结构--switch选择结构
if-switch 选择结构 switch结构可以更好的解决等值判断问题switch 选择结构的语法:switch (表达式){ case 常量 1: //代码块1: break; case 常量 2 ...
- java DDD 基于maven开发的探讨
对于DDD我目前的理解是 1.除了数据的基本操作,也可以把一些公用的方法或者类迁移到Infrastructrue 2.对于domain层可以声明各个聚合根的操作接口:例:IXXXRepository ...
- [ SHOI 2012 ] 随机树
\(\\\) \(Description\) 开始有一棵只有一个根节点的树.每次随机选择一个叶子节点,为他添上左右子节点,求: 生成一棵有\(N\)个叶节点的树,所有叶节点平均高度的期望. 生成一棵有 ...
- Quartz中时间参数说明 即Cron表达式
Cron表达式 Quartz使用类似于Linux下的Cron表达式定义时间规则,Cron表达式由6或7个由空格分隔的时间字段组成,如表1所示: 表1 Cron表达式时间字段 位置 时间域名 允许值 允 ...
- Java我来了
七天的C#集训,第一天接触Java,觉得很多相似的地方,尝试用eclipse码了几句(有些差别,毕竟没有写C#那么流畅),总体来说觉得还不错,对自己接下来要求是,更加熟练并且牢记Java的命令,更加深 ...
- [ECharts]"echarts/config" is not exists
今天在给Echarts折线图中的数据点增加点击事件的时候总是出现一个 Uncaught Error: [MODULE_MISS]"echarts/config" is not ex ...