hdu 1595 find the longest of the shortest(dijstra + 枚举)
来源:互联网 发布:itunes更新软件 编辑:程序博客网 时间:2024/04/29 05:14
http://acm.hdu.edu.cn/showproblem.php?pid=1595
大致题意:
给一个图,让输出从中删除任意一条边后所得最短路径中最长的。。
思路:
直接枚举每条边想必是不行的。其实有些边是不需要枚举的,因为删除它们并不影响起点到终点的最短路。起作用的边都是未删边前的最短路径上的边,删除它们最短距离肯定增大,只需在这些最短距离中求最大的即可。
记录最短路径上的边,只需一个pre数组记录松弛时每个点的前驱节点。
#include <stdio.h>#include <algorithm>#include <set>#include <map>#include <vector>#include <math.h>#include <string.h>#define LL long long#define _LL __int64using namespace std;const int maxn = 1010;const int INF = 0x3f3f3f3f;int n,m;int mapp[1010][1010];int dis[maxn],vis[maxn];int pre[maxn],pre1[maxn];int ans;void init(){ for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(i == j) mapp[i][j] = 0; else mapp[i][j] = INF; } } memset(pre,-1,sizeof(pre));}int dijstra(int s){ memset(dis,INF,sizeof(dis)); memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) { dis[i] = mapp[s][i]; pre[i] = s; } vis[s] = 1; pre[s] = -1; for(int i = 1; i < n; i++) { int min = INF,pos; for(int j = 1; j <= n; j++) { if(min > dis[j] && !vis[j]) { min = dis[j]; pos = j; } } vis[pos] = 1; for(int j = 1; j <= n; j++) { if(!vis[j] && dis[j] > dis[pos] + mapp[pos][j]) { dis[j] = dis[pos] + mapp[pos][j]; pre[j] = pos; } } } return dis[n];}void solve(){ memcpy(pre1,pre,sizeof(pre)); ans = -1; int u = n; while(pre1[u] != -1) { int tmp = mapp[pre1[u]][u]; mapp[pre1[u]][u] = mapp[u][pre1[u]] = INF; //删除该边 int res = dijstra(1); if(res != INF) ans = max(ans,res); mapp[ pre1[u] ][u] = mapp[u][ pre1[u] ] = tmp; u = pre1[u]; } printf("%d\n",ans);}int main(){ int u,v,w; while(~scanf("%d %d",&n,&m)) { init(); for(int i = 0; i < m; i++) { scanf("%d %d %d",&u,&v,&w); mapp[u][v] = mapp[v][u] = min(w,mapp[u][v]); } dijstra(1); solve(); } return 0;}
0 0
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- hdu 1595 find the longest of the shortest ( spfa + 枚举 )
- find the longest of the shortest (hdu 1595 SPFA+枚举)
- 杭电 1595 find the longest of the shortest Dijstra
- HDU 1595 find the longest of the shortest(枚举,最短路)
- hdu 1595 find the longest of the shortest(最短路+枚举)
- HDU 1595find 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)
- find the longest of the shortest HDU
- find the longest of the shortest HDU
- Hdu 1595 find the longest of the shortest 枚举+最短路
- 【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 (最短路+记录路径+枚举删边)
- hdu 1595 find the longest of the shortest 最短路dijkstra+枚举
- hdu 1595 find the longest of the shortest【最短路+枚举】
- 大家好!
- JavaWeb多线程简单Web服务器实现
- Ubuntu 安装 VMware Tools 报“无法找到kernel header path”的解决方案
- j2ee的线程安全--threadlocal
- A Memory-Efficient Doubly Linked List
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- 最新Struts漏洞修复方案
- C++ Interview question
- gen kernel cscope.files 2.6.22.6
- Reverse Linked List II
- busy box 1.7.0 cscope.files
- C\C++的转义字符
- 互联网本质论(欢迎来论)
- vfork详细讲解和实例