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

POJ——T 3225 Roadblocks

时间:2017-08-23 22:11:30      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:search   int   hose   moved   include   return   技术分享   link   pre   

http://poj.org/problem?id=3255

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15673   Accepted: 5507

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

 
技术分享
 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <queue>
 4 
 5 using namespace std;
 6 
 7 const int INF(0x3f3f3f3f);
 8 const int N(50000+15);
 9 const int M(100000+5);
10 
11 int hed[N],had[N],sumedge;
12 struct Edge
13 {
14     int v,next,w;
15 }edge1[M],edge2[M];
16 inline void ins(int u,int v,int w)
17 {
18     edge1[++sumedge].v=v;
19     edge1[sumedge].next=hed[u];
20     edge1[sumedge].w=w;
21     hed[u]=sumedge;
22     edge2[sumedge].v=u;
23     edge2[sumedge].next=had[v];
24     edge2[sumedge].w=w;
25     had[v]=sumedge;
26 }
27 
28 int dis[N];
29 bool inq[N];
30 void SPFA(int s)
31 {
32     for(int i=1;i<s;i++) dis[i]=INF;
33     dis[s]=0; inq[s]=1;
34     queue<int>que; que.push(s);
35     for(int u,v;!que.empty();)
36     {
37         u=que.front(); que.pop(); inq[u]=0;
38         for(int i=had[u];i;i=edge2[i].next)
39         {
40             v=edge2[i].v;
41             if(dis[v]>dis[u]+edge2[i].w)
42             {
43                 dis[v]=dis[u]+edge2[i].w;
44                 if(!inq[v]) que.push(v),inq[v]=1;
45             }
46         }
47     }
48 }
49 
50 struct Node
51 {
52     int now,g;
53     friend bool operator < (Node a,Node b)
54     {
55         return a.g+dis[a.now]>b.g+dis[b.now];
56     }
57 };
58 int Astar(int s,int t,int k)
59 {
60     priority_queue<Node>que;
61     int cnt=0;  Node u,v;
62     u.g=0; u.now=s;
63     que.push(u);
64     for(;!que.empty();)
65     {
66         u=que.top(); que.pop();
67         if(u.now==t) cnt++;
68         if(cnt==k) return u.g;
69         for(int i=hed[u.now];i;i=edge1[i].next)
70         {
71             v.now=edge1[i].v;
72             v.g=u.g+edge1[i].w;
73             que.push(v);
74         }
75     }
76 }
77 
78 inline void read(int &x)
79 {
80     x=0; register char ch=getchar();
81     for(;ch>9||ch<0;) ch=getchar();
82     for(;ch>=0&&ch<=9;ch=getchar()) x=x*10+ch-0;
83 }
84 
85 int AC()
86 {
87     int n,m; read(n),read(m);
88     for(int v,u,w;m--;)
89         read(u),read(v),read(w),ins(u,v,w);
90     SPFA(n);    printf("%d\n",Astar(1,n,2));
91     return 0;
92 }
93 
94 int I_want_AC=AC();
95 int main(){;}
WA掉的A*

 

POJ——T 3225 Roadblocks

标签:search   int   hose   moved   include   return   技术分享   link   pre   

原文地址:http://www.cnblogs.com/Shy-key/p/7420439.html

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