poj 3255
来源:互联网 发布:淘宝怎么买弹簧刀 编辑:程序博客网 时间:2024/06/06 06:45
Roadblocks
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 intersectionN.
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, andD that describe a road that connects intersectionsA andB and has lengthD (1 ≤ D ≤ 5000)
Output
Sample Input
4 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
求次短路。。。dijstra算法好。。。短。。。
这个做法就是多加了个d2[],求次短路。。
k短。。。不是很清楚,本星期搞定吧
#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>using namespace std;#define MAXN (5000+5)#define INF 0x3f3f3f3ftypedef pair<int, int> P;struct node{ int v, dis;};int n;vector<node> G[MAXN];int d[MAXN], d2[MAXN];void dijstra(int s){ priority_queue<P, vector<P>, greater<P> > q; for(int i = 1; i <= n; i++) d[i] = d2[i] = INF; d[s] = 0; q.push(P(d[s], s)); while(!q.empty()){ P now = q.top(); q.pop(); int u = now.second, dis = now.first; if(d2[u] < dis) continue; for(int i = 0; i < G[u].size(); i++){ int v = G[u][i].v, c2 = dis+G[u][i].dis; if(d[v] > c2){ swap(d[v], c2); q.push(P(d[v], v)); } if(d2[v] > c2 && d[v] < c2){ d2[v] = c2; q.push(P(d2[v], v)); } } }}int main(){ freopen("test.in", "r", stdin); freopen("test.out", "w", stdout); int m; scanf("%d%d", &n, &m); for(int i = 1; i <= m; i++){ int u, v, dis; scanf("%d%d%d", &u, &v, &dis); G[u].push_back((node){v, dis}); G[v].push_back((node){u, dis}); } dijstra(1); printf("%d\n", d2[n]); return 0;}
COME ON!
- POJ 3255
- poj 3255
- poj 3255
- poj 3255
- POJ 3255
- POJ 3255
- poj 3255
- poj 3255
- poj 3255
- poj 3255
- POJ 3255
- poj 3255
- POJ 3255
- poj 3255 Roadblocks
- poj 3255 Roadblocks
- poj 3255 求次大最短路
- POJ-3255-Roadblocks
- POJ 3255 Roadblocks
- HDU 2074 叠筐
- poj 3352 Road Construction
- Tomcat 配置gzip无效
- 安卓studio打印出日志输出
- tjut 2433
- poj 3255
- Debug Howto - Android-x86
- sql 按照数字排序
- CodeForces 610BVika and Squares
- Android wifi无线调试App新玩法ADB WIFI
- JAVA多线程实现的三种方式
- 定宽定高情况下块级元素居中的方法
- android 6.0 运行时权限框架
- 互斥体CMutex的使用