POJ3255(次小生成树)
来源:互联网 发布:深圳壹叁叁网络老挝 编辑:程序博客网 时间:2024/05/01 18:26
次小生成树裸题
到某个点的次小次短路要么是其它某个点u的最短路加上u->v的边,要么就是到u的次短路加上u->v的边,因此所需要求的就是到所有顶点的最短路和次短路
#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;const int maxn = 5000 + 10;const int INF = 1<<30;typedef pair<int, int> P;int N, R;struct edge{ int to, cost;};vector<edge> G[maxn];int dist[maxn]; //最短路径int dist2[maxn]; //次短路经void solve(){ priority_queue<P, vector<P>, greater<P> > que; fill(dist, dist+N, INF); //最短距离 fill(dist2, dist2+N, INF); //次短距离 dist[0] = 0; que.push(P(0,0)); while(!que.empty()) { P p = que.top(); que.pop(); int v = p.second, d = p.first; if(dist2[v] < d) continue; for(int i=0; i<G[v].size(); ++i) { edge e = G[v][i]; int d2 = d + e.cost; if(dist[e.to]>d2) { swap(dist[e.to], d2); que.push(P(dist[e.to], e.to)); } if(dist2[e.to] >d2&&dist[e.to] <d2) { dist2[e.to] = d2; que.push(P(dist2[e.to], e.to)); } } } printf("%d\n",dist2[N-1]);}int main(){ int i, a, b, c; edge e; while(~scanf("%d%d",&N,&R)) { for(i=0; i<R; ++i) { scanf("%d%d%d", &a, &b, &c); a--,b--; e.to = b, e.cost = c; G[a].push_back(e); e.to = a; G[b].push_back(e); } solve(); } return 0;}
0 0
- POJ3255(次小生成树)
- poj3255 次短生成树
- poj3255 Roadblocks (次短路)
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- ObjectiveC中的block用法解析
- 浅谈对JS闭包的理解
- 关于ajax的下载被浏览器阻止的问题
- [转载] 机器学习科普文章:“一文读懂机器学习,大数据/自然语言处理/算法全有了”
- 关于下拉框
- POJ3255(次小生成树)
- Fragment中监听onKey事件
- CCD/CMOS靶面尺寸型号标准
- 分支-05. 用天平找小球(10)
- Android四大组件之—— 使用服务进行后台操作
- linux 查看 端口3306
- android studio 下guava混淆配置
- 工具[git]_如何使用git创建项目,创建分支
- iOS AutoLayout自动布局中级开发教程(8)-VisualFormat可视化格式语言创建约束