POJ 3255 Roadblocks (dijkstra最短路)
来源:互联网 发布:剑灵灵女最美捏脸数据 编辑:程序博客网 时间:2024/05/01 12:17
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
dijkstra求次短路 先更新最短路 然后看次短路能否更新
AC代码如下:
//// POJ 3255 Roadblocks//// Created by TaoSama on 2015-03-20// Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>#define CLR(x,y) memset(x, y, sizeof(x))using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;int n, r, dp[5005], dp2[5005];struct Edge {int to, cost;};vector<Edge> G[N];typedef pair<int, int> Sta; //距离, 节点标号int dijkstra() {priority_queue<Sta, vector<Sta>, greater<Sta> > pq;memset(dp, 0x3f, sizeof dp); memset(dp2, 0x3f, sizeof dp2);dp[1] = 0; pq.push(Sta(0, 1));while(!pq.empty()) {Sta p = pq.top(); pq.pop();int u = p.second, d = p.first;if(d > dp2[u]) continue;for(int i = 0; i < G[u].size(); ++i) {Edge &e = G[u][i];int d2 = d + e.cost;if(dp[e.to] > d2) {swap(dp[e.to], d2);pq.push(Sta(dp[e.to], e.to));}if(dp2[e.to] > d2 && dp[e.to] < d2) {dp2[e.to] = d2;pq.push(Sta(dp2[e.to], e.to));}}}return dp2[n];}int main() {#ifdef LOCALfreopen("in.txt", "r", stdin);//freopen("out.txt","w",stdout);#endif//ios_base::sync_with_stdio(0);cin >> n >> r;for(int i = 1; i <= r; ++i) {int x, y, v; cin >> x >> y >> v;G[x].push_back((Edge) {y, v});G[y].push_back((Edge) {x, v});}cout << dijkstra() << endl;return 0;}
- POJ 3255 Roadblocks (dijkstra最短路)
- POJ - 3255 Roadblocks(最短路Dijkstra算法求次最短路)
- POJ 3255 Roadblocks 最短路Dijkstra+堆优化
- POJ 3255 Roadblocks (Dijkstra求次短路)
- POJ 3255 RoadBlocks(次短路+dijkstra变形)
- poj 3255 Roadblocks Dijkstra求次短路
- [POJ 3255]Roadblocks[dijkstra][次短路]
- poj 3255 Roadblocks (次短路+dijkstra)
- POJ 3255 Roadblocks(Dijkstra)
- POJ 3255 Roadblocks(dijkstra+邻接表求解次短路问题)
- POJ 3255-Roadblocks [次短路 Dijkstra] 《挑战程序设计竞赛》2.5
- POJ 3255 Roadblocks(次短路,Dijkstra变形+邻接表存储)
- POJ 题目3255Roadblocks(次短路)
- POJ 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks(次短路)
- poj 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks (次短路问题)
- POJ 3255 Roadblocks(次短路)
- sqlserver2008安装出现跨语言
- 动作识别之STIP(Space-Time Interest point)(七)
- PHP异常测试,示例二原创
- 515B. Drazil and His Happy Friends
- SpringMVC之Controller传递JSON数据到页面
- POJ 3255 Roadblocks (dijkstra最短路)
- fckeditor常用配置
- 烦乱随笔
- c#面向对象(五)----类的字段与属性
- Jsoup模拟登陆小例子
- Eclipse安装反编译工具
- Codeforces Round #295 (Div. 2) A. Pangram
- 最简单的视频网站(JavaEE+FFmpeg)
- expect脚本入门