码迷,mamicode.com
首页 > 数据库 > 详细

Roadblocks -次短路

时间:2017-08-23 21:40:35      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:sample   pre   first   wap   次短路   algorithm   too   ret   count   

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

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

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

Source

 
求次短路 
可以用A*
这里我用的dijs+堆优化
题目有点坑 
这个可以来回跑。。 从1->3->1->...->n;的路径合法
SPFA好像没法重复跑 
所以我WA了又WA 就改了dijs
 
技术分享
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <ctype.h>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <queue>
 7 
 8 using namespace std;
 9 
10 const int MAXN=5010;
11 const int INF=1e9;
12 
13 int n,m;
14 
15 int dis[MAXN],dis2[MAXN];
16 
17 typedef pair<int,int> P;
18 
19 struct edge {
20     int to;
21     int val;
22 };
23 vector<edge> G[MAXN];
24 
25 inline void read(int&x) {
26     int f=1;register char c=getchar();
27     for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar());
28     for(;isdigit(c);x=x*10+c-48,c=getchar());
29     x=x*f;
30 }
31 
32 inline void SPFA() {
33     priority_queue<P,vector<P>,greater<P> >Q;
34     for(int i=0;i<=n;++i) dis[i]=dis2[i]=INF;
35     Q.push(P(1,0));
36     dis[1]=0;
37     while(!Q.empty()) {
38         P u=Q.top();
39         Q.pop();
40         int v=u.first,V=u.second;
41         if(dis2[v]<V) continue;
42         for(int i=0;i<G[v].size();++i) {
43             edge e=G[v][i];
44             int d=V+e.val;
45             if(dis[e.to]>d) {
46                 swap(dis[e.to],d);
47                 Q.push(P(e.to,dis[e.to]));
48             }
49             if(dis2[e.to]>d&&d>dis[e.to]) {
50                 dis2[e.to]=d;
51                 Q.push(P(e.to,dis2[e.to]));
52             }
53         }
54     }
55     return;
56 }
57 
58 int hh() {
59 //    freopen("1.in","r",stdin);
60 //    freopen("2.out","w",stdout);
61     read(n);read(m);
62     for(int x;m--;) {
63         edge p;
64         read(x);read(p.to);read(p.val);
65 //        --x,--p.to;
66         G[x].push_back(p);
67         swap(p.to,x);
68         G[x].push_back(p);
69     }
70     SPFA();
71     printf("%d\n",dis2[n]);
72     return 0;
73 }
74 
75 int sb=hh();
76 int main() {;}
代码

 

Roadblocks -次短路

标签:sample   pre   first   wap   次短路   algorithm   too   ret   count   

原文地址:http://www.cnblogs.com/whistle13326/p/7420163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!