HDU 1595 find the longest of the shortest
2013-09-18 12:58
316 查看
题意:对于题目给定的一个图,问去掉起点到终点的最短路径上的某一条边之后起点到终点的最短距离里的最大值。
思路:先计算原图的最短路径,保存最短路径。枚举最短路径每一条边,删掉该边,然后计算最短路径,保留最大的那个即可。
实现:先用一个spfa求得最短路径,用一个路径数组保存路径。然后枚举每一条路径上的边,用另一个spfa实现对该边的假删除(即对其进行标记,表示现在删的是此边)。然后比较得出最长的那个最短路的值,将其输出。
代码:
思路:先计算原图的最短路径,保存最短路径。枚举最短路径每一条边,删掉该边,然后计算最短路径,保留最大的那个即可。
实现:先用一个spfa求得最短路径,用一个路径数组保存路径。然后枚举每一条路径上的边,用另一个spfa实现对该边的假删除(即对其进行标记,表示现在删的是此边)。然后比较得出最长的那个最短路的值,将其输出。
代码:
#include<cstdio> #include<iostream> #include<cstring> #include<queue> using namespace std; #define N 1010 #define inf 0x7fffffff bool mark ; bool used ; int p ; int d ; int pre ; struct node { int v,cost,next; } e[N*N]; int idx; int n,m; queue<int>que; void add(int u,int v,int c) { e[idx].v=v; e[idx].cost=c; e[idx].next=p[u]; p[u]=idx++; } void spfa() { int i,j; int u,v; for(i=1; i<=n; i++) { mark[i]=false; d[i]=inf; } while(!que.empty()) que.pop(); mark[1]=true; d[1]=0; que.push(1); while(!que.empty()) { u=que.front(); que.pop(); mark[u]=false; for(i=p[u]; i!=-1; i=e[i].next) { v=e[i].v; if(d[v]>d[u]+e[i].cost) { d[v]=d[u]+e[i].cost; pre[v]=u; if(!mark[v]) { mark[v]=true; que.push(v); } } } } } int work() { int i,j; int u,v; for(i=1; i<=n; i++) { mark[i]=false; d[i]=inf; } while(!que.empty()) que.pop(); mark[1]=true; d[1]=0; que.push(1); while(!que.empty()) { u=que.front(); que.pop(); mark[u]=false; for(i=p[u]; i!=-1; i=e[i].next) { v=e[i].v; if(used[u][v]&&d[v]>d[u]+e[i].cost) { d[v]=d[u]+e[i].cost; if(!mark[v]) { mark[v]=true; que.push(v); } } } } return d ; } int main() { int i,j; int a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { idx=0; memset(p,-1,sizeof(p)); pre[1]=-1; memset(used,0,sizeof(used)); for(i=0; i<m; i++) { scanf("%d%d%d",&a,&b,&c); used[a][b]=1; used[b][a]=1; add(a,b,c); add(b,a,c); } int ans=0; spfa(); for(int t=n; pre[t]!=-1; t=pre[t]) { used[t][pre[t]]=0; used[pre[t]][t]=0; int h=work(); if(h>ans) ans=h; used[t][pre[t]]=1; used[pre[t]][t]=1; } printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
- hdu 1595 find the longest of the shortest 最短路dijkstra+枚举
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest ( spfa + 枚举 )
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- find the longest of the shortest HDU - 1595
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- hdu 1595 find the longest of the shortest (枚举+最短路)
- HDU1595 find the longest of the shortest[最短路]
- hdu 1595 find the longest of the shortest(最短路spfa)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- HDU 1595 find the longest of the shortest(Dijkstra)
- hdu 1595 find the longest of the shortest (dijkstra + spfa)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest(dijkstra)
- 【HDU】1595 find the longest of the shortest 枚举+最短路
- hdu 1595 find the longest of the shortest