转载请注明出处: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. 最小生成树---->prim算法的应用 hdu1863

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. 51nod1819 黑白树V2

    简单的题面 给定一棵以1为根的有根树,点可能是黑色或白色,操作如下. 1. 选定一个点x,将x的子树中所有到x的距离为奇数的点的颜色反转.2. 选定一个点x,将点x的颜色反转.3. 选定一个点x,询问 ...

  3. bzoj 3573: [Hnoi2014]米特运输

    3573: [Hnoi2014]米特运输 Description 米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题.    D星 ...

  4. 使用SQL语句将数据库中的两个表合并成一张表

    select * into 新表名 from (select * from T1 union all select * from T2)  这个语句可以实现将合并的数据追加到一个新表中. 不合并重复数 ...

  5. git -- git clone

    git clone 命令参数: usage: git clone [options] [--] <repo> [<dir>] -v, --verbose be more ver ...

  6. VK Cup 2016 - Round 1 (Div. 2 Edition) A. Bear and Reverse Radewoosh 水题

    A. Bear and Reverse Radewoosh 题目连接: http://www.codeforces.com/contest/658/problem/A Description Lima ...

  7. MySQL5.7添加授权账号及修改默认端口

    1.修改默认端口 打开配置文件 vim /etc/my.cnf 分别添加端口在client.mysql节点 [client] port=15099 [mysqld] port=15099 需要注意se ...

  8. windows2008服务器连接Oracle慢的问题。

    昨天发布程序到2008服务器的IIS,从Sql Server数据库取数没问题,但是从Oracle数据库取数,非常的慢,同样的程序在2003服务器上没问题,本机也没问题.一开始怀疑是这台机器有问题,后来 ...

  9. Java中文件与字节数组转换

    注:来源于JavaEye 文件转化为字节数组: http://www.javaeye.com/topic/304980 /** * 文件转化为字节数组 * * @param file * @retur ...

  10. vue头像上传与文件压缩

    工作中遇到的问题记录:vue开发头像上传组件,后端提供接口,需求为可相册上传,可相机拍摄上传,文件大小限制为2M 需求点分析 移动端调用相册/摄像头实现拍照 图片压缩,当前高像素的相机拍出来的图片都有 ...