poj 3255 Roadblocks(次最短路径)
来源:互联网 发布:学拼音打字软件下载 编辑:程序博客网 时间:2024/05/16 05:26
Description
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
Hint
#include <stdio.h>#include <iostream>#include <string.h>#include <math.h>#include <ctype.h>#include <algorithm>#include <map>#include <math.h>#include <stack>#include <queue>#define Max 6000#define inf 100000000using namespace std;int N,R;struct edge{ int to; int cost;};vector<edge> G[Max];int dis[Max];int dis2[Max];typedef pair<int, int> P;void solve(){ priority_queue<P,vector<P>,greater<P> > q; fill(dis, dis+N, inf); fill(dis2, dis2+N, inf); dis[0] = 0; q.push(P(0,0)); while (!q.empty()) { P p = q.top();q.pop(); int v = p.second, d = p.first; if (dis2[v]<d) { continue; } for (int i=0; i<G[v].size(); i++) { edge &e = G[v][i]; int d2 = e.cost + d; if (dis[e.to]>d2) { swap(dis[e.to], d2); q.push(P(dis[e.to],e.to)); } if (dis2[e.to]>d2&&dis[e.to]<d2) { dis2[e.to] = d2; q.push(P(dis2[e.to],e.to)); } } } printf("%d\n",dis2[N-1]);}int main(){ while (scanf("%d%d",&N,&R)!=EOF) { for (int i=0; i<=N; i++) { G[i].clear(); } for (int i=0; i<R; i++) { int s,e,c; scanf("%d%d%d",&s,&e,&c); s--,e--; edge p; p.to = e,p.cost = c; G[s].push_back(p); p.to = s; G[e].push_back(p); } solve(); } return 0;}
- poj 3255 Roadblocks(次最短路径)
- POJ, 3255 Roadblocks(次短路径)
- POJ 3255 Roadblocks (次短路径 + Dijkstra算法)
- poj——3255Roadblocks(优先队列 求最小次最短路径)
- POJ 3255 Roadblocks --次短路径
- POJ 3255 Roadblocks(Dijkstra)
- poj 3255 Roadblocks (SPFA)
- POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- [POJ](3255)Roadblocks---- 最(次)短路径(邻接表+dijkstra优先队列优化)
- POJ 3255 Roadblocks (dijkstra最短路)
- POJ 题目3255Roadblocks(次短路)
- POJ 3255 Roadblocks(次短路)
- POJ 3255-Roadblocks(A*+spfa)
- POJ 3255 Roadblocks(次短路)
- poj 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks (次短路问题)
- POJ 3255 Roadblocks(次短路)
- ZOJ 2966 Build The Electric System(最小生成树)
- redis单机搭建--详细
- 2、Maven在MyEclipse的集成,并建立一个简单的Maven项目
- BZOJ-2875 随机数生成器 矩阵乘法快速幂+快速乘
- Web之一 JS
- poj 3255 Roadblocks(次最短路径)
- Theano 学习笔记 Basics:Baby Steps - Algebra
- thinkphp源码解析 (Version 3.1) -4 /Lib/Core/Dispatcher.class
- jq开发插件格式
- sql笔记-postgreSQL
- 前段js滑动到最底端触发加载下一页的方法
- Check list
- 使用apktool 2.0 反编译和批量打包
- canvas 圆点组成的汉字闪烁,