首页 > 代码库 > POJ 3255 Roadblocks

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 41 2 1002 4 2002 3 2503 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 long14 #define pii pair<long long,int>15 #define INF 0x3f3f3f3f16 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 }
View Code

 

POJ 3255 Roadblocks