转载请注明出处: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【次短路】的更多相关文章

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

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

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

  4. hdu 1595 find the longest of the shortest

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...

  5. hdu 1595 find the longest of the shortest(dijstra + 枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...

  6. hdu1595find the longest of the shortest 最短路

    //给一个无向图,问删除一条边,使得从1到n的最短路最长 //问这个最长路 //这个删除的边必定在最短路上,假设不在.那么走这条最短路肯定比其它短 //枚举删除这条最短路的边,找其最长的即为答案 #i ...

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

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

  9. hdu 1595(最短路变形好题)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. Redis学习篇(十)之排序

    SORT 按照键值从小到大或者从大到小的顺序进行排序 对数字进行排序 语法:SORT key [DESC] 默认情况下,是升序排序,可以指定DESC进行降序排序 对字母进行排序 语法:SORT key ...

  2. Scrapy实战篇(四)之周杰伦到底唱了啥

    从小到大,一直很喜欢听周杰伦唱的歌,可是相信很多人和我一样,并不能完全听明白歌词究竟是什么,今天我们就来研究一下周董最喜欢在歌词中用的词,这一小节的构思是这样的,我们爬取周杰伦的歌词信息,并且将其进行 ...

  3. BZOJ 3676: [Apio2014]回文串 回文树 回文自动机

    http://www.lydsy.com/JudgeOnline/problem.php?id=3676 另一种更简单更快常数更小的写法,很神奇……背板子. #include<iostream& ...

  4. 【轮廓线DP】POJ2411-Mondriaan's Dream

    今天美国的院士过来讲课XD以为会很无聊但是谜之好听,而且英语基本上都听懂了的样子♪(´▽`) 逃到图书馆来写解题报告 [题目大意] 给出一个m*n的方格,用2*1的骨牌覆盖有几种情况. [思路] 最基 ...

  5. YanghuiTriangle

    Demand 1 用实现循环队列 2 参考PPT用循环队列打印杨辉三角 3 用JDB或IDEA单步跟踪排队情况,画出队列变化图,包含自己的学号信息 4 把代码推送到代码托管平台 5 把完成过程写一篇博 ...

  6. String是最基本的数据类型吗?

    不是, 基本数据类型包括:byte,short,int,long,float,double,boolean,char. 而String是类代表字符串,属于引用类型,所谓引用类型包括:类,接口,数组.. ...

  7. JAVA容器-重点总结与深度解析

    重点内容总结 问题思考 问题思考解析

  8. CentOS下KVM克隆完成后修改MAC地址/VMware复制虚拟机修改MAC地址

    克隆完成之后可能mac地址会有冲突,进入KVM删除/etc/udev/rules.d/70-persistent-net.rules中的eth0的配置,接着把eth1改成eth0,并且修改/etc/s ...

  9. ELM322 - OBD (VPW) to RS232 Interpreter (v2.0)

    http://elmelectronics.com/DSheets/ELM322DS.pdf

  10. SQL Server 2000 ——DBCC命令

    http://blog.163.com/ruifeng_00/blog/static/6904584200971291923462/   一.定义 微软当初从SYBASE将DBCC是作为数据库一致性检 ...