Poj Roadblocks(次短路)

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 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

USACO 2006 November Gold

记最短路径长度为dis[ ],次短路径长度为ddis[ ],则dis[v] = min( dis[u] + cost[u][v],  ddis[u] + cost[u][v] ),所以我们只需要计算出最短路径和次短路径即可。这就跟最短路径不一样了,在实现Dijkstra算法的时候,我们既要记录最短路径,还要记录次短路径。

代码:

 1 #include <vector>
 2 #include <map>
 3 #include <set>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstdlib>
 9 #include <string>
10 #include <cstring>
11 #include <queue>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 #define MAX 5055
15
16 int N,R;
17 struct node{
18     int to;
19     int cost;
20 };
21 vector<node> g[MAX];
22 int dis[MAX],ddis[MAX];
23
24 void solve()
25 {
26     typedef pair<int,int> P;
27     priority_queue<P,vector<P>,greater<P> > que;
28     fill(dis,dis+N,INF);
29     fill(ddis,ddis+N,INF);
30     dis[0]=0;
31     que.push(P(0,0));
32
33     while(!que.empty()){
34         P p=que.top();  que.pop();
35         int v=p.second,d1=p.first;
36         if(ddis[v]<d1)  continue;
37         for(int i=0; i<g[v].size(); i++){
38             node s=g[v][i];
39             int d2=d1+s.cost;
40             if(dis[s.to]>d2){
41                 swap(dis[s.to],d2);
42                 que.push(P(dis[s.to],s.to));
43             }
44             if(ddis[s.to]>d2 && dis[s.to]<d2){
45                 ddis[s.to]=d2;
46                 que.push(P(ddis[s.to],s.to));
47             }
48         }
49     }
50     printf("%d\n",ddis[N-1]);
51 }
52
53 int main()
54 {
55     int from,to,cost;
56     node e;
57     while(~scanf("%d%d",&N,&R)){
58         while(R--){
59             scanf("%d%d%d",&from,&to,&cost);
60             from--;to--;
61             e.to=to;e.cost=cost;
62             g[from].push_back(e);
63             e.to=from;
64             g[to].push_back(e);
65         }
66         solve();
67     }
68 }
时间: 2024-12-24 15:47:43

Poj Roadblocks(次短路)的相关文章

POJ 3255 Roadblocks (次短路问题)

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

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 1502 最短路+坑爹题意

链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5249   Accepted: 3237 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed share

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)条双向

POJ 2448(K短路,A*+SPFA) Remmarguts&#39; Date

题意 给一个n个点m条边的图,然后给一个起点和一个终点,求起点到终点的第K短路. 思路 求第K短路.一个经典的问题. SPFA+A* 核心思想在A*搜索的估计函数的建立上. F(x) = g(x) + h(x) 估价函数 = s到x的距离 + x到t的距离 估价函数的含义就是经过x这个点的路径的距离. 我们在搜索的时候每次选择估价函数较小的值,进行拓展.这样我们搜索到t点的状态出来顺序就是,最短路-次短路-.第三短路- 就减少了我们搜索的状态数. 代码实现上,实现一个估价函数的结构体,然后是实现

poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135 建模分两部分:1.如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路 2.如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置 到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种 上代码 /**********************************

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

给出1-N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了 题目: I - RoadblocksTime 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 o

BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )

从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ----------------------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue&g

It&amp;#39;s not a Bug, It&amp;#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1353   Accepted: 516 Description It is a curious fact that consumers buying a new software product generally do not expect the software to