转载请注明出处: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. [2]树的DFS序

    定义: 树的DFS序就是在对树进行DFS的时候,对树的节点进行重新编号:DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题. 代码: void ...

  2. 【BZOJ 3442】 3442: 学习小组 (最大费用流)

    3442: 学习小组 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 403  Solved: 193 Description [背景] 坑校准备鼓励学生 ...

  3. HDU 6138 Fleet of the Eternal Throne(AC自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6138 [题目大意] 给出一些串,询问第x个串和第y个串的公共子串, 同时要求该公共子串为某个串的前 ...

  4. [HDU5492]Find a path

    题目大意: 一个n*m的格子,每个格子上都有一个数. 你可以向下或者向右走,从(1,1)走到(n,m),问方差*(n+m-1)最小的路径是哪个? 思路: 方差*(n+m-1)就相当于给格子里每个数乘上 ...

  5. python一行代码开启http

    python -m SimpleHTTPServer 8000 & 监听8000端口 浏览器用127.0.0.1:8000访问 如果出现no module named SimpleHTTPSe ...

  6. DiskFileUpload上传与Spring的CommonsMultipartResolver上传对比

    最近在做一个小小的上传功能竟被虐得体无完肤, 在使用tomcat内置的DiskFileUpload获取前台jsp传来的附件参数和其他表单参数, 竟然出现莫名其妙的乱码问题, 即使tomcat的serv ...

  7. JDK及JRE中bin工具说明

    jre/bin工具说明:javac:Java编译器,将Java源代码换成字节代 java:Java解释器,直接从类文件执行Java应用程序代码 appletviewer(小程序浏览器):一种执行HTM ...

  8. busdog is a filter driver for MS Windows (XP and above) to sniff USB traffic.

    https://code.google.com/p/busdog/ busdog is a filter driver for MS Windows (XP and above) to sniff U ...

  9. chrome插件开发-----------将网址转化成二维码website2QRcode

    微信自带的浏览器无法输入链接,仅仅能通过扫描二维码实现.可是有时候看到一个有趣的站点,想分享,还得先去将链接转化成二维码的站点.先转成二维码.再扫描.有点麻烦.所以写了一个插件.直接生成二维码. 须要 ...

  10. redis.conf配置解释

    daemonize:如果需要在后台运行,把该项改为yespidfile:配置多个pid的地址,默认在/var/run/redis.pidbind:绑定ip,设置后只接受来自该ip的请求port:监听端 ...