POJ3255:Roadblocks(次短路 SPFA+A星)

给出1~N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了

题目:

I - 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 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
lengthD (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)

代码:


  1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <queue>
5 using namespace std;
6
7 #define MAXN 5000+10
8
9
10 int N,R;
11
12 #define MP(a,b) make_pair(a,b)
13 #define INF 100000000
14
15 #define pii pair<int,int>
16 vector<pii> G[MAXN];
17 struct pri
18 {
19 bool operator () (const pair<int,int> &p1,const pair<int,int>&p2)
20 {
21 return p1.second< p2.second;
22 }
23 };
24
25 int d[MAXN];
26 void dj(int s)
27 {
28 for(int i = 1;i<=N;i++)
29 {
30 d[i]= INF;
31 d[i] = INF;
32 }
33 d[s]=0;
34 d[s]=0;
35
36 priority_queue<pii,vector<pii>,pri> q;
37 q.push( MP(s,d[s]) );
38 while(!q.empty())
39 {
40 int u = q.top().first;
41 q.pop();
42
43 int size = G[u].size();
44
45 for(int i=0;i<size;i++)
46 {
47 int v,w;
48 v = G[u][i].first;
49 w = G[u][i].second;
50
51 if( d[v] > d[u]+w )
52 {
53 d[v]=d[u]+w;
54 q.push(MP(v,d[v]));
55
56 }
57 }
58 }
59
60 }
61 /*
62 void spfa(int src)
63 {
64
65 for(int i=1;i<=N;i++)
66 {
67 d[i] = INF;
68 }
69 d[src]=0;
70 queue<int> q;
71 q.push(src);
72 int a,next;
73 int vis[MAXN]={0};
74 while(!q.empty())
75 {
76 a = q.front();
77 q.pop();
78 vis[a] = 0;
79 int len = G[a].size();
80 for(int i=0;i<len;i++)
81 {
82 next = G[a][i].first;
83 if( d[next]> d[a]+G[a][i].second )
84 {
85 d[next] = d[a]+G[a][i].second;
86 if( !vis[next] )
87 {
88 q.push(next);
89 vis[next]=1;
90 }
91 }
92 }
93 }
94 }
95 */
96 struct node{
97
98 int now,g,h,f;
99 bool operator < (const node& x) const
100 {
101 if( f == x.f )
102 {
103 return x.g<g;
104 }
105 else
106 {
107 return x.f<f;
108 }
109 }
110 };
111 void astar(int src,int to)
112 {
113 priority_queue<node> q;
114 node a, next;
115
116 a.now = src;
117 a.g = 0;
118 a.f = d[src]+a.g;
119 q.push(a);
120 int cnt = 0;
121 while(!q.empty())
122 {
123 a = q.top();
124 q.pop();
125 if( a.now == to )
126 {
127 cnt++;
128 if( cnt == 2 )
129 {
130 cout<<a.g<<endl;
131 return;
132 }
133 }
134 int size = G[a.now].size();
135 for(int i=0;i<size;i++)
136 {
137 next.now = G[a.now][i].first;
138 next.g = a.g + G[a.now][i].second;
139 next.f = next.g+d[next.now];
140 q.push(next);
141 }
142 }
143 }
144
145 int main()
146 {
147
148 cin>>N>>R;
149
150 for(int i=0;i<R;i++)
151 {
152 int f,t,w;
153 cin>>f>>t>>w;
154
155 G[f].push_back( MP(t,w) );
156 G[t].push_back( MP(f,w) );
157 }
158
159 dj(N);
160 // spfa(N);
161 astar(1,N);
162 return 0;
163 }

POJ3255:Roadblocks(次短路 SPFA+A星),码迷,mamicode.com

时间: 2024-10-14 00:52:37

POJ3255:Roadblocks(次短路 SPFA+A星)的相关文章

poj3255 Roadblocks 次短路

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

POJ3255 Roadblocks 次短路Dijkstra做法

有段时间没做题了,这几天一直在寻找感觉,尽量多看书,这题目就是n个地方,编号从1到n,然后有r条路,问你从1号到达n号地方的次短路长度为多少,同一条边可以重复走,直接在dijkstra算法里同时记录一个次短路就可以了,但是一直WA,后来去看了讨论面板,那里有人给了测试数据,我不知道那数据的对错,但是干扰了我很久,也许那些数据实在题目案例之外的吧,对我的程序没有任何影响,我只是太久没错 一时 忘了双向边了,一开始只建立了单向边~代码写的也比较冗长~ int n,r; typedef struct

bzoj1726 [Usaco2006 Nov]Roadblocks第二短路

1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 826  Solved: 394[Submit][Status] Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路. 贝茜所在的乡村有R(1<=R<=100,000)条双向

POJ 3255 Roadblocks (次短路问题)

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

最短路 spfa算法

问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个整数u, v, l,表示u到v有一条长度为l的边. 输出格式 共n-1行,第i行表示1号点到i+1号点的最短路. 样例输入 3 31 2 -12 3 -13 1 2 样例输出 -1-2 数据规模与约定 对于10%的数据,n = 2,m = 2. 对于30%的数据,n <= 5,m <= 10. 对

TOJ--2674--最短路(spfa)

厌死了......排位 晋级赛 两连跪 ... 三角形 的那题还是 无限WA  ... 还有 明天又要早起... 先还是来看下这题吧 话说 好久没写 最短路了 --------- spfa 是我最喜欢的最短路版本 touch me 这题 其实相比其它的最短路 还是有个很让人看不懂的地方---让我纠结了很久 首先 它告诉了我们城市的编号 是从 1~1000 然后 又突然告诉我们 哪些城市和我们的出发地相邻 假如是 1 10 100...之类的 最后 告诉我们 你想取哪些城市 我就在想 起点呢!!!

洛谷P1462 通往奥格瑞玛的道路 二分答案+最短路SPFA

洛谷P1462 通往奥格瑞玛的道路二分答案+最短路SPFA 二分交费最多的一次的钱数 然后只将符合要求的边加入图中 如果到终点的最短路大于等于血量 或者直接起点不能到达终点那么说明不符合要求 需要加大答案 时间复杂度 (log答案)* Ek 需要注意如果本来就不能到达 那么直接输出AFK 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define For(i,j,k) for(int i=j;i<=k;i++) 4 using

1726: [Usaco2006 Nov]Roadblocks第二短路

1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 835  Solved: 398[Submit][Status] Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路. 贝茜所在的乡村有R(1<=R<=100,000)条双向

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co