题面:[USACO06NOV] Roadblocks G

大秦为你打开题目传送门

题面翻译

贝茜把家搬到了一个小农场,但她常常回到 FJ 的农场去拜访她的朋友。贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路。
贝茜所在的乡村有 $R(1\le R \le 10^5)$ 条双向道路,每条路都联结了所有的 $N(1\le N\le 5000)$ 个农场中的某两个。贝茜居住在农场 $1$,她的朋友们居住在农场 $N$(即贝茜每次旅行的目的地)。
贝茜选择的第二短的路径中,可以包含任何一条在最短路中出现的道路,并且,一条路可以重复走多次。当然咯,第二短路的长度必须严格大于最短路(可能有多条)的长度,但它的长度必须不大于所有除最短路外的路径的长度。

题目描述

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

输入格式

Line 1: Two space-separated integers: N and R

Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

输出格式

Line 1: The length of the second shortest path between node 1 and node N

样例 #1

样例输入 #1

1
2
3
4
5
4 4
1 2 100
2 4 200
2 3 250
3 4 100

样例输出 #1

1
450

提示

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

思路

次短路。我们只要在求最短路的同时,不断地去更新次短路就可以了。具体的细节就是,再求次短路的时候,就不可以像原来一样用 vis 标记了。因为在我的博客里曾经提到过,vis 的主要作用是保证最短路只扩散一次,但是我们现在求次短路,次短路也是要不断更新的,而且次短路可以去更新别的次短路,也就是说,vis 标记这个是用不了的。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
// typedef __int128 int128;
typedef pair<int , int > pii;
typedef unsigned long long ull;

namespace FastIO
{
// char buf[1 << 20], *p1, *p2;
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
template<typename T> inline T read(T& x) {
x = 0;
int f = 1;
char ch;
while (!isdigit(ch = getchar())) if (ch == '-') f = -1;
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x *= f;
return x;
}
template<typename T, typename... Args> inline void read(T& x, Args &...x_) {
read(x);
read(x_...);
return;
}
inline ll read() {
ll x;
read(x);
return x;
}
};
using namespace FastIO;

const int MaxV = 5010;
const int MaxE = 1e5 + 10;

template<int N, int M>
class Graph {
private :
struct Edge {
int to, nt, wt;
Edge() {}
Edge(int to, int nt, int wt) : to(to), nt(nt), wt(wt) {}
}e[M];
int hd[N], cnte;
public :
inline void AddEdge(int u, int v, int w = 0) {
e[++cnte] = Edge(v, hd[u], w);
hd[u] = cnte;
}
inline int head(int u) { return hd[u]; }
inline int nt(int u) { return e[u].nt; }
inline int to(int u) { return e[u].to; }
inline int wt(int u) { return e[u].wt; }
};

int n, m;
Graph< MaxV, MaxE << 1 >G;

inline void Input() {
read(n, m);
int u, v, w;
for(int i = 1; i <= m; i++) {
read(u, v, w);
G.AddEdge(u, v, w);
G.AddEdge(v, u, w);
}
}

int dis[MaxV][2];
int vis[MaxV];

inline void Dijkstra() {
priority_queue<pii, vector<pii >, greater<pii > >q;
q.push(make_pair(0, 1));
for(int i = 1; i <= n; i++) {
dis[i][0] = dis[i][1] = 1e9;
}
dis[1][0] = 0;
while(!q.empty()) {
int d = q.top().first;
int u = q.top().second; q.pop();
// if(vis[u]) continue;
// vis[u] = 1;
for(int i = G.head(u); i; i = G.nt(i)) {
int v = G.to(i), nd = d + G.wt(i);
if(dis[v][0] > nd) {
dis[v][1] = dis[v][0];
dis[v][0] = nd;
q.push(make_pair(dis[v][0], v));
}
if(dis[v][0] < nd && dis[v][1] > nd) {
dis[v][1] = nd;
q.push(make_pair(dis[v][1], v));
}
}
}
}

inline void Work() {
Dijkstra();
printf("%d\n", dis[n][1]);
}

int main() {
int T = 1;
while(T--) {
Input();
Work();
}
return 0;
}