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 ...
随机推荐
- iOS 9音频应用播放音频之音量设置与声道设置
iOS 9音频应用播放音频之音量设置与声道设置 iOS 9音频应用音量设置 音量又称响度.音强,是指人耳对所听到的声音大小强弱的主观感受,其客观评价尺度是声音的振幅大小.在iOS 9音频应用的应用中, ...
- python 异步 select pooll epoll
概念: 首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当se ...
- jQuery before 和 after
A.after(B) ==== B.insertAfter(A) B 放在 A 的后面A.before(B) ==== B.insertBefore(A) B 放在 A 的前面 A.append(B) ...
- QT学习笔记2:QT中常用函数
一.QString转number QString number() QString number() QString number() QString number() QString number( ...
- CodeForces - 1009D Relatively Prime Graph
题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...
- [HDU1290]献给杭电五十周年校庆的礼物
[HDU1290]献给杭电五十周年校庆的礼物 题目大意: 问\(n(n\le1000)\)个平面能够将一个三维空间分成几部分. 思路: 公式\(\frac{n^3+5n+6}6\). 源代码: #in ...
- 线程,线程安全与python的GIL锁
今天看到一篇文章,讲述的是几个提升python性能的项目:传送门 在看的过程中,接触到一个名词,一个从学python开始就一直看到,但是从来都是一知半解的名词,心里不开心,必须把它搞明白,对了,这个词 ...
- 简单破解 Sencha Architect 2.2 (ExtJs Designer)
Sencha Architect 2是ExtJS和Sencha Touch的官方可视化IDE工具.最新版本是2.2,说是破解,其实是修改License来实现无限试用而已. 1.先下载安装官方软件,大约 ...
- CSS的outline属性
input标签的outline的三个属性: outline-color outline-style outline-width 当设置input的focus状态的时候可以使用. input:focus ...
- Linux下使用ISC DHCP可以实现动态推送静态路由表
ISC DHCP可以实现动态推送静态路由表,先做个记号. 参考: https://gauvain.pocentek.net/docs/dhcpd-push-routes/ http://www.isc ...