POJ 3255 Roadblocks

Roadblocks

Time Limit: 2000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 3255
64-bit integer IO format: %lld      Java class name: Main

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

Line 1: Two space-separated integers: N and R
Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Source

USACO 2006 November Gold

解题:次短路模板题。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<long long,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 struct arc{
18     int to,w;
19     arc(int x = 0,int y = 0){
20         to = x;
21         w = y;
22     }
23 };
24 const int maxn = 5010;
25 vector<arc>g[maxn];
26 int d[maxn],d2[maxn],n,m;
27 priority_queue< pii,vector< pii >,greater< pii > >q;
28 void dijkstra(){
29     for(int i = 0; i <= n; i++) d[i] = d2[i] = INF;
30     while(!q.empty()) q.pop();
31     d[1] = 0;
32     q.push(make_pair(d[1],1));
33     while(!q.empty()){
34         int u = q.top().second;
35         int w = q.top().first;
36         q.pop();
37         if(d2[u] < w) continue;
38         for(int i = 0; i < g[u].size(); i++){
39             int dis = w + g[u][i].w;
40             if(d[g[u][i].to] > dis){
41                 swap(dis,d[g[u][i].to]);
42                 q.push(make_pair(d[g[u][i].to],g[u][i].to));
43             }
44             if(d2[g[u][i].to] > dis && d[g[u][i].to] < dis){
45                 d2[g[u][i].to] = dis;
46                 q.push(make_pair(d2[g[u][i].to],g[u][i].to));
47             }
48         }
49     }
50 }
51 int main(){
52     int u,v,w;
53     while(~scanf("%d %d",&n,&m)){
54         for(int i = 0; i <= n; i++) g[i].clear();
55         for(int i = 0; i < m; i++){
56             scanf("%d %d %d",&u,&v,&w);
57             g[u].push_back(arc(v,w));
58             g[v].push_back(arc(u,w));
59         }
60         dijkstra();
61         printf("%d\n",d2[n]);
62     }
63     return 0;
64 }

时间: 2024-10-30 16:50:21

POJ 3255 Roadblocks的相关文章

POJ 3255 Roadblocks (次短路问题)

解法有很多奇葩的地方,比如可以到达终点再跳回去再跳回来(比如有两个点)....反正就是不能有最短路,不过没关系,算法都能给出正确结果 思想:和求最短路上的点套路一样,spfa先正着求一次,再反着求一次最短路,然后枚举每条边<i,j>找dist_zheng[i] + len<i,j> + dist_fan[j]的第二小值即可!注意不能用邻接矩阵,那样会MLE,应该用邻接表 /* poj 3255 3808K 266MS */ #include<cstdio> #inclu

poj 3255 Roadblocks【次短路】

题目:poj 3255 Roadblocks 题意:给出一个无向图,然后求1到n点的次短路 分析:两种做法,第一种,Astat+最短路求k短路的方法. 第二种是比较暴力的方法. 先求1点到所有点的最短路dis1 然后求n点到所有点的最短路dis2 然后枚举所有边,则次短路为dis1[from] + dis2[to] + w[i]中大于最短路的最短的. AC代码: #include <cstdio> #include <string> #include <cstring>

POJ - 3255 Roadblocks (次短路)

POJ - 3255 Roadblocks Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status 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

POJ 3255 Roadblocks (次短路模板)

Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       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 quick

POJ 3255 Roadblocks (次短路径 + Dijkstra算法)

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7982   Accepted: 2921 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 q

POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 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

POJ 3255 Roadblocks Dijkstra 算法变形

#include <cstdio> #include <iostream> #include <queue> using namespace std; const int INF = 1000000; const int maxn = 5005; struct edge{ int y,w; edge(int cy,int ww){ y = cy; w = ww; } }; vector<edge> vec[maxn]; int n,m; struct num

POJ 3255 &amp;&amp; HDU 1688 &amp;&amp; HDU 3191 次短路问题

POJ 3255 Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7627   Accepted: 2798 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 h

如何用求次长边——POJ 3255 题解

题目大意 给你一个 \(n\) 个点,\(m\) 条边的无向图,求出这个无向图中从1到n的次短路.其中\(n \le 5000\),\(m \le 100000\). 题目传送门 POJ 3255 思路 其实求次长路是很简单的一个问题,但是网上却有很多算法都过于复杂了.首先我们先求出从1到每个结点的最短路长度(用Dijkstra或者是SPFA都可以),存入数组 \(dis1\) 中,然后再求出从结点 \(n\) 到任意结点的最短路的长度,存入数组 \(dis2\) 中. 然后我们枚举这个图中的所