hdu 1595 find the longest of the shortest (spfa)
来源:互联网 发布:掷骰子抽奖js特效 编辑:程序博客网 时间:2024/05/02 02:12
记录最短路上的路径依次枚举删掉,看看剩下的最短路最大值是多少。
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;#define N 1005const int inf = 1 << 28;struct node{ int to, nxt, w;}e[N*N];int dis[N], vis[N];int head[N];int pre[N];int n, m;int cnt;int a[N][N];void init(){ memset(head, -1, sizeof(head)); memset(pre, 0, sizeof(pre)); memset(a, 0, sizeof(a)); cnt = 0;}void add( int u, int v, int w ){ e[cnt].to = v; e[cnt].w = w; e[cnt].nxt = head[u]; head[u] = cnt++;}void spfa(){queue<int> q;while( !q.empty() )q.pop();for( int i = 1; i <= n; i++ ){vis[i] = 0;dis[i] = inf;}vis[1] = 1;dis[1] = 0;q.push(1);while( !q.empty() ){int now = q.front();q.pop();vis[now] = 0;for( int i = head[now]; ~i; i = e[i].nxt ){int to = e[i].to;if( dis[to] > dis[now] + e[i].w ){dis[to] = dis[now] + e[i].w;pre[to] = now;if( !vis[to] ){vis[to] = 1;q.push(to);}}}}}void spfa1(){queue<int> q;while( !q.empty() )q.pop();for( int i = 1; i <= n; i++ ){vis[i] = 0;dis[i] = inf;}vis[1] = 1;dis[1] = 0;q.push(1);while( !q.empty() ){int now = q.front();q.pop();vis[now] = 0;for( int i = head[now]; ~i; i = e[i].nxt ){int to = e[i].to;if( a[now][to] && dis[to] > dis[now] + e[i].w ){dis[to] = dis[now] + e[i].w;//pre[to] = now;if( !vis[to] ){vis[to] = 1;q.push(to);}}}}}int main(){while(~scanf("%d%d", &n, &m)){init();int u, v, w;while(m--){scanf("%d%d%d", &u, &v, &w);add(u, v, w);add(v, u, w);a[u][v] = a[v][u] = 1;}int ans = 0;spfa();for( int i = n; i != 0; i = pre[i]){a[i][pre[i]] = a[pre[i]][i] = 0;spfa1();ans = max(ans, dis[n]);a[i][pre[i]] = a[pre[i]][i] = 1;}printf("%d\n", ans);}return 0;}
某处vis标记写错改了好久好久好久好久好久好久好久好久好久好久好久好久好久好久好久好久好久好久好久好久...
0 0
- 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 ( spfa + 枚举 )
- find the longest of the shortest (hdu 1595 SPFA+枚举)
- hdu 1595 find the longest of the shortest (dijkstra + spfa)
- HDU 1595 find the longest of the shortest (Dijkstra)
- 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)(存储并枚举最短路径的边)
- 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
- 1595 hdu find the longest of the shortest
- hdu 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest
- HDU 1595——find the longest of the shortest
- eclipse的package explore树形结构
- js常用方法总结-url中文乱码问题
- Eclipse右键New菜单项的自定义设置
- Html5中不在适用的html4的属性总结
- 第十五周项目二-阅读程序1
- hdu 1595 find the longest of the shortest (spfa)
- hdu 5122 树状数组?..其实是乱搞
- Get Start StrangeIOC for Unity3D
- 黑马程序员——java加强之注解
- 教皇修改之后丢失的十天
- eclipse中汉字过小的解决办法
- 重装win7后如何恢复ubuntu引导
- 防止页面客户端被缓存
- hdu45221——小明系列问题——小明序列 线段树优化dp