【LG3238】 [HNOI2014]道路堵塞
题目描述
给你一张\(N\)个点、\(M\)条边的有向图,按顺序给定你一条有\(L\)条边的\(1\rightarrow n\)的最短路,
每次断掉这\(L\)条边中的一条(不对后面答案产生影响),求每次断边之后的最短路。
题解
40pts
每次断边之后跑\(dijkstra\)最短路即可,复杂度\(O(LM\log N)\)。
100pts
法一:
好像是一种奇怪的堆+\(spfa\)算法,但是在现在这种卡\(spfa\)的大环境下,这种方法已经不对了。
法二:
分别建正图和反图跑dijkstra,记一个点\(i\)在起点为\(1\)的正图上最短距离为\(dis_i\),
在起点为\(N\)的反图上最短距离为\(dis'_i\),可以
枚举每一条不是那\(L\)条边中的边\(e_{a\rightarrow b}\),可以知道过这一条边的最短路长度为\(dis_a+dis'_b\)+\(e_{a\rightarrow b}\)的长度。
设给定的\(L\)条边分别为\(e_1,e_2...e_L\)。
则经过这一条边的最短路序列为:
\(e_1\rightarrow ...\;\rightarrow e_i\rightarrow\)一些奇怪的边\(\rightarrow e_{a\rightarrow b}\rightarrow\)一些奇怪的边\(\rightarrow e_j\rightarrow ...\;\rightarrow e_L\)
其中\(1\leq i<j\leq L\)。
那么对于\(i+1\)到\(j-1\)的所有边,我们断掉他们时,可以通过一条长度为\(\;\;dis_a+dis'_b\)+\(e_{a\rightarrow b}\)的长度\(\;\;\)的路径到达。
则在线段树上在区间\([i+1,j-1]\)打一个路径长度的标记,对于每一个\(e_i\),单点查询即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int INF = 1e9;
const int MAX_N = 1e5 + 5, MAX_M = 2e5 + 5;
struct Edge { int u, v, w; } e[MAX_M];
struct Graph { int to, cost, num; } ;
vector<Graph> G[2][MAX_N];
int N, M, L, sp[MAX_N], dis[2][MAX_N], prv[2][MAX_N], used[MAX_M];
int chk(int x, int y, bool op) { return op ? max(x, y) : min(x, y); }
void dijkstra(int s, int op, int t) {
static priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
for (int i = 1; i <= N; i++) dis[op][i] = INF;
que.push(make_pair(dis[op][s] = 0, s));
while (!que.empty()) {
pair<int, int> p = que.top(); que.pop();
int x = p.second;
if (p.first < dis[op][x]) continue;
if (x == t) continue;
for (int i = 0, sz = G[op][x].size(); i < sz; i++) {
Graph &e = G[op][x][i]; int v = e.to;
if (dis[op][x] + e.cost < dis[op][v]) {
prv[op][v] = (op ? 0 : INF);
dis[op][v] = dis[op][x] + e.cost;
if (!used[e.num]) prv[op][v] = chk(prv[op][v], prv[op][x], op);
else prv[op][v] = chk(prv[op][v], used[e.num], op);
que.push(make_pair(dis[op][v], v));
} else if (dis[op][x] + e.cost == dis[op][v]) {
if (!used[e.num]) prv[op][v] = chk(prv[op][v], prv[op][x], op);
else prv[op][v] = chk(prv[op][v], used[e.num], op);
}
}
}
}
#define lson (o << 1)
#define rson (o << 1 | 1)
int val[MAX_M << 2], tag[MAX_M << 2];
void puttag(int o, int v) { val[o] = min(val[o], v); tag[o] = min(tag[o], v); }
void pushdown(int o, int l, int r) {
if (l == r || tag[o] == INF) return ;
puttag(lson, tag[o]); puttag(rson, tag[o]);
tag[o] = INF;
}
void pushup(int o) { val[o] = min(val[lson], val[rson]); }
void modify(int o, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) return (void)(puttag(o, v));
pushdown(o, l, r);
int mid = (l + r) >> 1;
if (ql <= mid) modify(lson, l, mid, ql, qr, v);
if (qr > mid) modify(rson, mid + 1, r, ql, qr, v);
pushup(o);
}
int query(int o, int l, int r, int pos) {
pushdown(o, l, r);
if (l == r) return val[o];
int mid = (l + r) >> 1;
if (pos <= mid) return query(lson, l, mid, pos);
else return query(rson, mid + 1, r, pos);
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
N = gi(), M = gi(), L = gi();
for (int i = 1; i <= M; i++) {
int u = gi(), v = gi(), w = gi();
G[0][u].push_back((Graph){v, w, i});
G[1][v].push_back((Graph){u, w, i});
e[i] = (Edge){u, v, w};
}
for (int i = 1; i <= L; i++) used[sp[i] = gi()] = i;
for (int i = 1; i <= N; i++) prv[0][i] = L + 1, prv[1][i] = 0;
prv[0][1] = 0, prv[1][N] = L + 1;
dijkstra(1, 0, N);
dijkstra(N, 1, 1);
for (int i = 1; i <= (L << 2); i++) val[i] = tag[i] = INF;
for (int i = 1; i <= M; i++) {
if (used[i] || dis[0][e[i].u] == INF || dis[1][e[i].v] == INF) continue;
int l = prv[0][e[i].u] + 1;
int r = prv[1][e[i].v] - 1;
if (l > r) continue;
else modify(1, 0, L + 1, l, r, dis[0][e[i].u] + dis[1][e[i].v] + e[i].w);
}
for (int i = 1; i <= L; i++) {
int ans = query(1, 0, L + 1, i);
printf("%d\n", (ans == INF) ? -1 : ans);
}
return 0;
}