Bzoj 1880(最短路+枚举/最短路图+拓扑排序+DP)

BZOJ 1880
题意:求出$s_1$到$t_1$的最短路径与$s_2$到$t_2$的最短路径的最长公共长度是多少。
解:我们求出四个点出发的单源最短路,然后枚举一条路径$(i,j)$,判断这条路径是否在两条最短路上,用公式算出公共长度,然后更新答案即可。
Hack,枚举的路径不一定在两条最短路径的公共路径上,必须要先预处理出每个点是否在公共路径上,因为每次枚举的最短路径端点都要在公共路径上。
知识点:本题运用了最短路加枚举,与CF 544D思路类似。

最短路图+拓扑排序+DP 做法:
最短路图:所有$dis(s,i)+dis(j,t)=dis(s,t)$的边$(i,j)$组成的 DAG 图
将$s_1$到$t_1$最短路图求出来并且和$s_2$到$t_2$的最短路径求交,然后在 DAG 图上按照拓扑序求最长路。

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
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<climits>
#include<queue>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define ULL unsigned long long
#define db double
const int MAXN = 1505, INF = 1000000000, Z = 1, F = 0;
struct edge {
int v, w, nxt;
}ed[2250000 + 5];
int en, hd[MAXN];
struct data {
int u, dis;
bool operator < (const data &b) const {return dis > b.dis;}
};
int n, m, s[2], t[2], dis[2][2][MAXN], vis[MAXN], whw[MAXN];
std::priority_queue<data > q;
inline void ins(int x, int y, int w) {
ed[++en] = (edge){y, w, hd[x]}, hd[x] = en;
ed[++en] = (edge){x, w, hd[y]}, hd[y] = en;
}
void dij(int Ty, int zf) {
ms(vis, 0);
if (zf == Z) q.push((data){s[Ty], 0}), dis[Ty][zf][s[Ty]] = 0;
else q.push((data){t[Ty], 0}), dis[Ty][zf][t[Ty]] = 0;
while (!q.empty()) {
data p = q.top(); q.pop();
if (vis[p.u]) continue;
vis[p.u] = 1;
for (int i = hd[p.u]; i > 0; i = ed[i].nxt) {
edge &e = ed[i];
if (dis[Ty][zf][e.v] > dis[Ty][zf][p.u] + e.w) {
dis[Ty][zf][e.v] = dis[Ty][zf][p.u] + e.w;
q.push((data){e.v, dis[Ty][zf][e.v]});
}
}
}
}
void clean() {
ms(whw, 0), ms(hd, -1);
for (int i = 0; i < 2; i++)
for (int j = 0; j <= n; j++) dis[i][0][j] = dis[i][1][j] = INF;
}
int solve() {
clean();
for (int x, y, w, i = 1; i <= m; i++) scanf("%d%d%d", &x, &y, &w), ins(x, y, w);
dij(0, Z), dij(1, Z), dij(0, F), dij(1, F);
for (int u = 1; u <= n; u++) {
for (int i = hd[u]; i > 0; i = ed[i].nxt) {
edge &e = ed[i];
if (dis[0][Z][u] + e.w + dis[0][F][e.v] == dis[0][Z][t[0]] && dis[1][Z][u] + e.w + dis[1][F][e.v] == dis[1][Z][t[1]]) whw[u] = whw[e.v] = 1;
if (dis[0][Z][u] + e.w + dis[0][F][e.v] == dis[0][Z][t[0]] && dis[1][Z][e.v] + e.w + dis[1][F][u] == dis[1][Z][t[1]]) whw[u] = whw[e.v] = 1;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!whw[i] || !whw[j]) continue;//不在公共路径
int &t1 = t[0], &t2 = t[1];
//1
int gg = dis[0][Z][t1] - dis[0][Z][i] - dis[0][F][j];
if ((dis[0][Z][t1] == dis[0][Z][i] + gg + dis[0][F][j]) && (dis[1][Z][t2] == dis[1][Z][i] + gg + dis[1][F][j]) && (gg > ans)) ans = gg;
//2
gg = dis[0][Z][t1] - dis[0][Z][i] - dis[0][F][j];
if ((dis[0][Z][t1] == dis[0][Z][i] + gg + dis[0][F][j]) && (dis[1][Z][t2] == dis[1][Z][j] + gg + dis[1][F][i]) && (gg > ans)) ans = gg;
}
}
printf("%d\n", ans);
return 0;
}
int main() {
scanf("%d%d%d%d%d%d", &n, &m, &s[0], &t[0], &s[1], &t[1]), solve();
return 0;
}
------ 本文结束 ------