HDU 1595 find the longest of the shortest【次短路】
转载请注明出处:http://blog.csdn.net/a1dark
分析:经典的次短路问题、dijkstra或者SPFA都能做、先找出最短路、然后依次删掉没条边、为何正确就不证明了、了解思想直接A掉、注意记录路径
#include<stdio.h>
#include<string.h>
#define INF 0x7ffffff
#define N 1010
int mpt[N][N];
int path[N];
int n,m;
void init(){
for(int i=1;i<N;i++){
for(int j=1;j<N;j++){
if(i==j)mpt[i][j]=0;
else mpt[i][j]=INF;
}
}
}
int dist[N];
void dij(){
int vis[N];
memset(path,-1,sizeof(path));
for(int i=1;i<=n;i++){
vis[i]=0;
dist[i]=mpt[1][i];
}
dist[1]=0;
vis[1]=1;
for(int i=1;i<n;i++){
int minx=INF;
int w=0;
for(int j=1;j<=n;j++){
if(vis[j]==0&&dist[j]<minx){
minx=dist[j];
w=j;
}
}
vis[w]=1;
for(int j=1;j<=n;j++){
if(vis[j]==0&&mpt[w][j]+dist[w]<dist[j]){
dist[j]=mpt[w][j]+dist[w];
path[j]=w;
}
}
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=0;i<m;i++){
int s,t,v;
scanf("%d%d%d",&s,&t,&v);
if(v<mpt[s][t]){
mpt[s][t]=v;
mpt[t][s]=v;
}
}
dij();
int x=n;
int dis[N];
dis[0]=n;
int len=1;
while(path[x]!=-1){
dis[len++]=path[x];
x=path[x];
}
dis[len++]=1;
int maxx=0;
for(int i=0;i<len-1;i++){
int temp=mpt[dis[i]][dis[i+1]];
mpt[dis[i]][dis[i+1]]=INF;
mpt[dis[i+1]][dis[i]]=INF;
dij();
if(dist[n]>maxx)
maxx=dist[n];
mpt[dis[i]][dis[i+1]]=temp;
mpt[dis[i+1]][dis[i]]=temp;
}
printf("%d\n",maxx);
}
return 0;
}
HDU 1595 find 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 ...
- 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 ...
- hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...
- hdu1595find the longest of the shortest 最短路
//给一个无向图,问删除一条边,使得从1到n的最短路最长 //问这个最长路 //这个删除的边必定在最短路上,假设不在.那么走这条最短路肯定比其它短 //枚举删除这条最短路的边,找其最长的即为答案 #i ...
- 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 1595(最短路变形好题)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
随机推荐
- SPOJ375 Query on a tree 【倍增,在线】
题目链接[http://www.spoj.com/problems/QTREE/] 题意:给出一个包含N(N<=10000)节点的无根树,有多次询问,询问的方式有两种1.DIST a b 求a ...
- BZOJ4552 HEOI2016排序
太棒了!思路很不错. 没想到HEOID1三道线段树. 这题我们可以二分答案,将小于他的在线段树中设成0,大于他的设成1然后模拟操作复杂度O(mlog^2n) By:大奕哥 #include<bi ...
- luoguP1354房间最短路问题
判断两点间连通性,建图跑floyed #include<bits/stdc++.h> using namespace std; ; struct node { ],x; }q[N];dou ...
- Apache URLRewrite 原理及配置实现
看一下网站上的一些 URL.您是否发现一些类似于 http://yoursite.com/info/dispEmployeeInfo. ... 99&type=summary的 URL?或者, ...
- Android 出现java.lang.NoClassDefFoundError错误
项目中用到了Retrofit在android4.4以下版本发生的问题 因为项目的build.gradle文件沿用于一个项目的,在defaultConfig z中已经声明了 multiDexEnable ...
- ACM -- 算法小结(一)利用数组存放实现排序
利用数组存放实现排序 hodj1425 321MS 2011/08 题意:输入n个数字,要求输出从大到小排序的前m个数 解题技巧:利用大数存储在数组后面,小数存储在前面,倒序输出完成从大 ...
- CSS的outline属性
input标签的outline的三个属性: outline-color outline-style outline-width 当设置input的focus状态的时候可以使用. input:focus ...
- OpenVPN配置网桥模式的一些理解
说明: 1.网桥的作用是将所有的网卡都能直连主机所在的路由器,可以把它当做一个独立的PC. 2.OpenVPN设置成网桥之后,IP设置成主机所在的IP网段,这样客户端连接进来之后就是所在主机所在的网络 ...
- TPS5410/TPS5430 开关电源稳压器(DC-DC)
- 静态资源(StaticResource)和动态资源(DynamicResource)
静态资源(StaticResource)和动态资源(DynamicResource) 资源可以作为静态资源或动态资源进行引用.这是通过使用 StaticResource 标记扩展或 DynamicRe ...