版本比较
LZM_最短路
正文
增加 1077 行删除 8 行
1233455678943 行未改动5354555657585960616257585960341 行未改动全部展开4024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480## 最短路摸底
我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬我是傻逼😡🤬
🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧🐧咕咕嘎嘎🐧
### T1
操你妈我真的气笑了。43 行未改动
### T2
我不会写 Spfa 😭😱🥵 我不会写 Spfa 😭😱🥵 我不会写 Spfa 😭😱🥵 我不会写 Spfa 😭😱🥵 我不会写 Spfa 😭😱🥵 我不会写 Spfa 😭😱🥵 我不会写 Spfa 😭😱🥵
**dijkstra:** 1. 贪心思想; 2. 不能应用于负权图; 341 行未改动全部展开 return 0;}```:::
## 最短路摸顶(?)
100 + 100 + 100 + 10 = 310,rk2,我是守银王。
DFbd 夺冠了太强了这集。
旅行者(昨天T5)把我带飞了。
### T1
神似昨天 T5,T3 也是神似。
P5304 简化版。
容易想到不论每一个值 $D$ 是多少,都是每一条边每一条边来考虑。
那么可以考虑先求出每一条边在 $D$ 为多大时才需要封闭。
令第 $i$ 条边需要封闭时的 $D$ 为 $D_i$。
那么对于第 $i$ 条边的终点,令它到达 $B$ 时的距离为 $jub_i$,同理令 $A$ 到第 $i$ 条边的起点的距离为 $jua_i$。
于是有 $jua_i + jub_i + L_i= D_i$。
发现 $L_i$ 已知,$jua_i$,$jub_i$ 均可以通过跑最短路得到(以 $A$ 为起点跑正图,以 $B$ 为起点跑反图)。
所以每一条边的 $D_i$ 均可以求出,再将其排序求前缀和,每一次询问时二分查找出最大的合法边,将其前缀和输出即可。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 3e5 + 5, INF = 1e18;int n, m, a, b, jua[N], jub[N], sum[N];struct zrr { int u, v, w, c;} l[N];struct node { int id, ju; bool operator < (const node A) const { return ju > A.ju; }};vector<node> g[N], g2[N];struct lll { int d, id;} k[N];bool cmp (lll qwe, lll ewq) { return qwe.d < ewq.d;}void dijka (int s) { memset(jua, 0x7f, sizeof(jua)); priority_queue<node> pq; pq.push({s, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop();// cout << t.id << " " << t.ju << "\n"; if (t.ju >= jua[t.id]) continue; jua[t.id] = t.ju; for (int i = 0; i < g[t.id].size(); i++) { pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju}); } }}void dijkb (int s) { memset(jub, 0x7f, sizeof(jub)); priority_queue<node> pq; pq.push({s, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop(); if (t.ju >= jub[t.id]) continue; jub[t.id] = t.ju; for (int i = 0; i < g2[t.id].size(); i++) { pq.push({g2[t.id][i].id, g2[t.id][i].ju + t.ju}); } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> a >> b; for (int i = 1; i <= m; i++) { cin >> l[i].u >> l[i].v >> l[i].w >> l[i].c; int u = l[i].u, v = l[i].v, w = l[i].w; g[u].push_back({v, w}); g2[v].push_back({u, w}); } dijka(a);// cout << "\n"; dijkb(b);// for (int i = 1; i <= n; i++) {// cout << jua[i] << " " << jub[i] << "\n";// } int ans = 0; for (int i = 1; i <= m; i++) { int lu = jua[l[i].u] + jub[l[i].v] + l[i].w; if (jua[l[i].u] > 1e18 || jub[l[i].v] > 1e18) lu = INF; k[i] = {lu, l[i].c};// cout << l[i].u << " " << l[i].v << " " << lu << "\n"; } sort(k + 1, k + m + 1, cmp); for (int i = 1; i <= m; i++) { sum[i] = sum[i - 1] + k[i].id; } int q; cin >> q; while(q--) { int d; cin >>d; int l = 0, r = m; while(l < r) { int mid = (l + r + 1) / 2; if (k[mid].d > d) r = mid - 1; else l = mid; } cout << sum[l] << "\n"; } return 0;} ```:::
### T2
其实也是昨天1T5 的弱化对吧。
首先多起点 bfs 找到危险城市。
然后跑点权dijk即可。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 3e5 + 5, INF = 1e12;int n, m, p, q, k, s, ju[N], du[N], d[N];struct node { int id, ju; bool operator < (const node A) const { return ju > A.ju; }};vector<node> g[N];void dijk () { memset(ju, 0x7f, sizeof(ju)); priority_queue<node> pq; for (int i = 1; i <= k; i++) pq.push({du[i], 0}); while(!pq.empty()) { node t = pq.top(); pq.pop(); if (t.ju >= ju[t.id]) continue; ju[t.id] = t.ju; for (int i = 0; i < g[t.id].size(); i++) { pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju}); } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k >> s >> p >> q; for (int i = 1; i <= k; i++) { cin >> du[i]; } for (int i = 1; i <= m; i++) { int u, v, w = 1; cin >> u >> v; g[u].push_back({v, w}); g[v].push_back({u, w}); } dijk(); for (int i = 1; i <= n; i++) { if (ju[i] == 0) d[i] = INF; else if (ju[i] <= s) { d[i] = q; } else { d[i] = p; } } d[n] = 0; d[1] = 0; memset(ju, 0x7f, sizeof(ju)); priority_queue<node> pq; pq.push({1, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop();// cout << t.ju << " " << t.id << "\n"; if (t.ju >= ju[t.id]) continue; ju[t.id] = t.ju; for (int i = 0; i < g[t.id].size(); i++) { pq.push({g[t.id][i].id, t.ju + d[t.id]}); } } cout << ju[n] << "\n"; return 0;} ```:::
### T3
这跟 T1 区别在哪。
同样找每一个点距离起终点的距离,然后如果存在本身就合法的情况直接输出。
剩下的每一个点枚举,求出需求的其他点到 b 的最大距离,同样的二分查找就做完了。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 3e5 + 5, INF = 1e18;int n, m, a, b, op, y, jua[N], jub[N], sum[N];struct zrr { int u, v, w;} l[N];struct node { int id, ju; bool operator < (const node A) const { return ju > A.ju; }};vector<node> g[N];struct lll { int d, id;} k[N];bool cmp (lll qwe, lll ewq) { return qwe.d < ewq.d;}void dijka (int s) { memset(jua, 0x7f, sizeof(jua)); priority_queue<node> pq; pq.push({s, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop();// cout << t.id << " " << t.ju << "\n"; if (t.ju >= jua[t.id]) continue; jua[t.id] = t.ju; for (int i = 0; i < g[t.id].size(); i++) { pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju}); } }}void dijkb (int s) { memset(jub, 0x7f, sizeof(jub)); priority_queue<node> pq; pq.push({s, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop(); if (t.ju >= jub[t.id]) continue; jub[t.id] = t.ju; for (int i = 0; i < g[t.id].size(); i++) { pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju}); } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> a >> b >> op >> y; y -= op; // 所以需要两端加起来小于等于 y for (int i = 1; i <= m; i++) { cin >> l[i].u >> l[i].v >> l[i].w; int u = l[i].u, v = l[i].v, w = l[i].w; g[u].push_back({v, w}); g[v].push_back({u, w}); } dijka(a);// cout << "\n"; dijkb(b); if (jua[b] <= y + op) { cout << n * (n - 1) / 2; return 0; }// for (int i = 1; i <= n; i++) {// cout << jua[i] << " " << jub[i] << "\n";// } int ans = 0; for (int i = 1; i <= n; i++) { k[i] = {jub[i], i}; // cout << jub[i] << " " <<i << "\n";// if (jua[i] + jub[i] <= y) ans--; }// cout << ans << "\n"; sort(k + 1, k + n + 1, cmp); for (int i = 1; i <= n; i++) { int d = y - jua[i]; int l = 0, r = n; while(l < r) { int mid = (l + r + 1) / 2;// cout << mid << " " << k[mid].d << "\n"; if (k[mid].d > d) r = mid - 1; else l = mid; } ans += l;// cout << d << " " << i << " " << l << "\n\n"; } cout << ans << "\n"; return 0;} ```:::
### T4
一个点如果可以进入,至少应该满足两个条件:
1. 不考虑屏障过去的时间已经达到。
2. 屏障城市都已经可以到达。
所以 dijk 时仅有都满足的点才入队,时间为这两项取 max。
其余都是常规的 dijk,但是需要注意满足一项时都应该单独循环一次,即总共有两次循环。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int INF = 1e18;int n, m, s, dis[100005], unblock[100005], b[100005], jie[100005];bool vis[100005];struct node { int id, ju; bool operator < (const node A) const { return ju > A.ju; }};vector<node> g[100005];vector<int> bao[100005];priority_queue<node> pq;void dijk () { memset(dis, 0x3f, sizeof(dis)); pq.push({1, 0}); dis[1] = 0; while(!pq.empty()) { int u = pq.top().ju, cur = pq.top().id; pq.pop(); if (vis[cur]) continue;// if (b[cur] > 0) continue; vis[cur] = 1; for (int i = 0; i < bao[cur].size(); i++) { int v = bao[cur][i]; jie[v] = max(jie[v], u); if (--b[v] == 0) { unblock[v] = jie[v]; if (dis[v] != INF) { pq.push({v, max(dis[v], unblock[v])}); } } } for (node r : g[cur]) { int v = r.id, w = r.ju; if (dis[v] > u + w) { dis[v] = u + w; if (b[v] == 0) { pq.push({v, max(dis[v], unblock[v])}); } } } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w});// g[v].push_back({u, w}); } for (int i = 1; i <= n; i++) { cin >> b[i]; for (int j = 1; j <= b[i]; j++) { int h; cin >> h; bao[h].push_back(i); } } dijk(); cout << max(dis[n], unblock[n]); return 0;}```:::
## 分层图思想
分层图一定要分层\😭/\😭/\😭/
### P4568
?!板板?!
和正常 dijk 唯一的区别在于多一个免费卷,所以考虑有 k 张免费卷就建 k + 1 层的图。
然后每一次转移增加一个上升一层花费为 0 的操作即可。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, s, q, z, ju[100005][15];struct node { int id, ju, juan; bool operator < (const node A) const { return ju > A.ju; }};vector<node> g[100005];priority_queue<node> pq;void dijk () { memset(ju, 0x7f, sizeof(ju)); pq.push({q, 0, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop(); // cout << t.id << " " << t.juan << " " << t.ju << "\n"; if (t.ju >= ju[t.id][t.juan]) continue; ju[t.id][t.juan] = t.ju; for (int i = 0; i < g[t.id].size(); i++) { pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju, t.juan}); if (t.juan != s) pq.push({g[t.id][i].id, t.ju, t.juan + 1}); } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> s; cin >> q >> z; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; u++; v ++; g[u].push_back({v, w}); g[v].push_back({u, w}); } q++; z++; dijk(); int ans = LONG_LONG_MAX; for (int i = 0; i <= s; i++) { ans = min(ans, ju[z][i]); } cout << ans << "\n"; return 0;}```:::
### P2939
和上一题差异在哪。
这集代码真的略了。
### P3119
代码是狗屎先写个思路。
首先想到缩点先缩掉所有环。(缩点模板)
然后再建一个同样的图放在第二层,反边用来链接两层。(分层图模板)
得到双层图直接跑最长路。(最长路模板)
傻逼拼好题我和你不共戴天。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 1e5 + 5;int n, m, ans, x[N], y[N];int edge[N], nxt[N], head[N], tot;int dfn[N], low[N], tt, st[N], top;bool ins[N];int col[N], cnt, w[N], f[2][N], d[N];bool vis[2][N];void add (int f, int t) { edge[++tot] = t, nxt[tot] = head[f], head[f] = tot; return;}void tarjan(int x) { dfn[x] = low[x] = ++tt; ins[x] = 1, st[++top] = x; for(int i = head[x]; i; i = nxt[i]) { int y = edge[i]; if(!dfn[y]) { tarjan(y); low[x] = min(low[x], low[y]); } else if(ins[y]) { low[x] = min(low[x], dfn[y]); } } if(dfn[x] == low[x]) { cnt++; int tmp = -1; while(tmp != x) { tmp = st[top--]; ++w[cnt], col[tmp] = cnt, ins[tmp] = 0; } } return;}void topo(int A) { queue<int> q; for(int i = 1; i <= cnt; i++) { if(!d[i]) q.push(i); } while(!q.empty()) { int x = q.front(); q.pop(); f[A][x] += w[x]; for(int i = head[x]; i; i = nxt[i]) { int y = edge[i]; d[y]--; if(vis[A][x]) { vis[A][y] = 1; f[A][y] = max(f[A][x], f[A][y]); } if(!d[y]) q.push(y); } } return;}signed main() { cin >> n >> m; for(int i = 1; i <= m; i++) { cin >> x[i] >> y[i]; add(x[i], y[i]); } for(int i = 1; i <= n; i++) { if(!dfn[i]) tarjan(i); } for(int i = 1; i <= m; i++) { x[i] = col[x[i]]; y[i] = col[y[i]]; } int s = col[1]; vis[0][s] = vis[1][s] = 1;
memset(head, 0, sizeof(head)); memset(d, 0, sizeof(d)); tot = 0; for(int i = 1; i <= m; i++) { if(x[i] != y[i]) { add(x[i], y[i]); d[y[i]]++; } } topo(0);
memset(head, 0, sizeof(head)); memset(d, 0, sizeof(d)); tot = 0; for(int i = 1; i <= m; i++) { if(x[i] != y[i]) { add(y[i], x[i]); d[x[i]]++; } } topo(1);
ans = w[s]; for(int i = 1; i <= m; i++) { if(vis[1][x[i]] && vis[0][y[i]]) { // cout << f[1][x[i]] << " " << f[0][y[i]] << " " << w[s] << "\n"; ans = max(ans, f[1][x[i]] + f[0][y[i]] - w[s]); } } cout << ans << "\n"; return 0;}```:::
### P4009
狗屎建图题\😭/我还记得你\😭/
1. 把油量看作一个层次,也就是三维立体最短路。
2. 定义 dis[x][y][p] 表示 (x, y) 时油量为 p 的最小花费。
3. 跑 dijk(当然我认为 dijk 不一定对我跑的 spfa)时只要产生任意变化(油量或者位置)都需要更新。
4. 花费是行驶 + 油费,相互独立不合并计入。
5. 其他的都是正常的最短路。
主要难点是建图:
- 四处乱跑要建图
```cppfor (int l = 1; l <= k; l++) { if (op && l > 1) break; if (i < n) add(i, j, l, i + 1, j, l + 1, 0); if (j < n) add(i, j, l, i, j + 1, l + 1, 0); if (i > 1) add(i, j, l, i - 1, j, l + 1, B); if (j > 1) add(i, j, l, i, j - 1, l + 1, B);}```
- 加油要建图
```cppfor (int l = 2; l <= k + 1; l++) add(i, j, l, i, j, 1, A + C * (!op));```
- 变油要建图
```cppfor (int l = 1; l <= k; l++) add(i, j, l, i, j, l + 1, 0);```
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 1e6 + 5, INF = 1e18;int n, k, A, B, C, tot;int h[N], ju[N], q[N];bool vis[N];struct node { int nxt, id, ju;} a[N];void add(int x1, int y1, int z1, int x2, int y2, int z2, int ju) { int last_ = n * n * (z1 - 1) + (x1 - 1) * n + y1; int next_ = n * n * (z2 - 1) + (x2 - 1) * n + y2; a[++tot] = (node) {h[last_], next_, ju}; h[last_] = tot;}void spfa() { memset(ju, 0x3f, sizeof(ju)); queue<int> q; q.push(1); ju[1] = 0; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = h[u]; i; i = a[i].nxt) { if (ju[a[i].id] > ju[u] + a[i].ju) { ju[a[i].id] = ju[u] + a[i].ju; if (!vis[a[i].id]) { vis[a[i].id] = 1; q.push(a[i].id); } } } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> A >> B >> C; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { bool op; cin >> op; for (int l = 1; l <= k; l++) add(i, j, l, i, j, l + 1, 0); for (int l = 2; l <= k + 1; l++) add(i, j, l, i, j, 1, A + C * (!op)); for (int l = 1; l <= k; l++) { if (op && l > 1) break; if (i < n) add(i, j, l, i + 1, j, l + 1, 0); if (j < n) add(i, j, l, i, j + 1, l + 1, 0); if (i > 1) add(i, j, l, i - 1, j, l + 1, B); if (j > 1) add(i, j, l, i, j - 1, l + 1, B); } } } spfa(); int ans = INF; for (int i = 1; i <= k + 1; i++) ans = min(ans, ju[n * n * i]); cout << ans << "\n"; return 0;}```:::
### P9751
同余最短路。
?!板板?!
:::success[code] ```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 1e5 + 5, INF = 1e18;vector<pair<int, int> > g[N];int n, m, k, ju[N][105], vis[N][105];struct node { int u, id, ju; bool operator < (const node & A) const { return ju > A.ju; }};signed main() { cin >> n >> m >> k; while (m--) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); } memset(ju, 0x3f, sizeof(ju)); priority_queue<node> q; q.push({1, 0, 0}); ju[1][0] = 0; while (!q.empty()) { int u = q.top().u, i = q.top().id; q.pop(); if (vis[u][i]) continue; vis[u][i] = 1; for (pair<int, int> h: g[u]) { int v = h.first, w = h.second, t = ju[u][i]; (t < w) && (t += (w - t + k - 1) / k * k); if (ju[v][(i + 1) % k] > t + 1) { q.push({v, (i + 1) % k, t + 1}); ju[v][(i + 1) % k] = t + 1; } } } cout << ((ju[n][0] > 1e9) ? (-1) : (ju[n][0])) << "\n"; return 0;}```:::
### CF677D
这题和一位。
## 分层图·周目二
### P4822
板子改一下转移层的代价。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, s, q, z, ju[55][55];struct node { int id, ju, juan; bool operator < (const node A) const { return ju > A.ju; }};vector<node> g[55];priority_queue<node> pq;void dijk () { memset(ju, 0x7f, sizeof(ju)); pq.push({q, 0, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop(); if (t.ju >= ju[t.id][t.juan]) continue; ju[t.id][t.juan] = t.ju; for (int i = 0; i < g[t.id].size(); i++) { pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju, t.juan}); if (t.juan != s) pq.push({g[t.id][i].id, g[t.id][i].ju / 2 + t.ju, t.juan + 1}); } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> s; q = 1, z = n; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); g[v].push_back({u, w}); } dijk(); int ans = LONG_LONG_MAX; for (int i = 0; i <= s; i++) { ans = min(ans, ju[z][i]); } cout << ans << "\n"; return 0;}```:::
### Johnson 全源最短路 与 势能分析
原来还有算法课堂。
约翰逊算法的定位:**存在负权但无负环**的**稀疏图**。以更优的复杂度求解全源最短路。
复杂度:$O(nm + n^2 log n)$,在稀疏图下比 floyd 更优。
算法步骤:
1. 按照原图来建图,并增加一个超级源点 S,与所有点距离为 0。
2. 跑一遍 SPFA 求出每一个点的势能。
3. 更新边的权值为 w` = w + h[u] - h[v],保证新边权为正数。
4. 直接跑一堆 dijk,注意超级源点要删掉。
5. 跑出来的距离还原 dis` = dis - h[u] + h[v]
### P5905
对于 Johnson 的模板
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, h[6005], ju[3005][3005], s;int u[6005], v[6005], w[6005];int cnt[6005], dis[6005];struct node { int id, ju; bool operator < (const node A) const { return ju > A.ju; }};vector<node> g[6005], ng[6005];bool vis[6005];queue<int> q;bool spfa() { memset(dis, 0x3f, sizeof(dis)); memset(cnt, 0, sizeof(cnt)); memset(vis, 0, sizeof(vis)); while(!q.empty()) q.pop(); q.push(0); dis[0] = 0; while(!q.empty()) { int x = q.front(); q.pop(); vis[x] = 0; for (int i = 0; i < g[x].size(); i++) { int y = g[x][i].id, z = g[x][i].ju; if (dis[x] + z < dis[y]) { dis[y] = dis[x] + z; if (!vis[y]) { vis[y] = 1; cnt[y] = cnt[x] + 1; if (cnt[y] > n) return 1; q.push(y); } } } } return 0;}void dijk () { // memset(ju, 0x7f, sizeof(ju)); priority_queue<node> pq; pq.push({s, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop(); if (t.ju >= ju[s][t.id]) continue; ju[s][t.id] = t.ju; for (int i = 0; i < ng[t.id].size(); i++) { pq.push({ng[t.id][i].id, ng[t.id][i].ju + t.ju}); } }}void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) g[0].push_back({i, 0}); for (int i = 1; i <= m; i++) { cin >> u[i] >> v[i] >> w[i]; g[u[i]].push_back({v[i], w[i]}); } if (spfa()) { cout << "-1\n"; return; } for (int i = 1; i <= n; i++) h[i] = dis[i]; for (int i = 1; i <= m; i++) { ng[u[i]].push_back({v[i], w[i] + h[u[i]] - h[v[i]]}); } memset(ju, 0x3f, sizeof(ju)); for (int i = 1; i <= n; i++) { s = i; dijk(); } for (int i = 1; i <= n; i++) { int ans = 0; for (int j = 1; j <= n; j++) { bool k = (ju[i][j] > 1e9); ju[i][j] = ju[i][j] - h[i] + h[j]; ju[i][j] = (k ? 1e9 : ju[i][j]); // cout << ju[i][j] << " "; ans += j * ju[i][j]; } cout << ans << "\n"; }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0;}```:::
### CF449B
1. 带上特殊边跑一次以首都为起点的 dijk,不带的话会 WA4,大致是因为有可能一条最短路不止用到一条特殊铁路。
2. 那么跑完之后枚举每一条特殊边,有各类情况:
- 最短路小于这条边,那么这条边必然拉完了,直接删除。
- 最短路等于这条边:
1. 这条边被其他恰好相等的情况所使用。如果 dijk 过程中这条边恰好等于最优的情况,那么记录它的一次贡献。 `if (ju[g[t.id][i].id] == g[t.id][i].ju + t.ju) vis[g[t.id][i].id]++;` 存入松弛情况时同样会计入一次贡献。 2. 枚举时如果发现它的贡献大于 1,那么同样可以删除,因为这说明它是可替代的,不一定是某条其他最短路的必要路径。 3. 由于有可能多条火车线连到同一个城市,所以每删除一次贡献值要减一,避免删的一条都不剩了。 :::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, k, s, ju[100005], id[100005], c[100005];struct node { int id, ju; bool operator < (const node A) const { return ju > A.ju; }};int vis[100005];bool flag[100005];vector<node> g[100005];priority_queue<node> pq;void dijk () { memset(ju, 0x7f, sizeof(ju)); pq.push({s, 0}); while(!pq.empty()) { node t = pq.top(); pq.pop(); if (flag[t.id]) continue; flag[t.id] = 1; for (int i = 0; i < g[t.id].size(); i++) { if (ju[g[t.id][i].id] == g[t.id][i].ju + t.ju) vis[g[t.id][i].id]++; if (ju[g[t.id][i].id] > g[t.id][i].ju + t.ju) { ju[g[t.id][i].id] = g[t.id][i].ju + t.ju; vis[g[t.id][i].id] = 1; pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju}); } } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; s = 1; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); g[v].push_back({u, w}); } for (int i = 1; i <= k; i++) { cin >> id[i] >> c[i]; g[1].push_back({id[i], c[i]}); g[id[i]].push_back({1, c[i]}); } dijk(); int ans = 0; for (int i = 1; i <= k; i++) { if (ju[id[i]] < c[i]) ans++; if (ju[id[i]] == c[i]) { if (vis[id[i]] > 1) { ans++; vis[id[i]]--; } } } cout << ans << "\n"; return 0;}```:::
### CF1650G
不是你告诉我这题只有绿题是吗。
首先发现答案应该等于最短路数量 $ans_1$ 和严格次短路数量 $ans_2$ 的和。
从起点出发跑广搜,得到 $dis1_i$ 和 $cnt1_i$ 分别表示 $s$ 到 $i$ 的最短距离和最短路径数量。
从终点出发跑广搜,得到 $dis2_i$ 和 $cnt2_i$ 分别表示 $t$ 到 $i$ 的最短距离和最短路径数量。
容易发现 $ans_1 = cnt1_t$。
因此对于每一条边 $i$ 有几种可能。
- $dis1_v = dis1_u + 1$。
- $dis1_u = dis1_v + 1$。
- $dis1_u = dis1_v$。
注意到仅有 $dis1_u = dis1_v$ 的情况有可能产生严格次短路,从而产生贡献。
1. $v$ 在最短路上,即 $dis1_v + dis2_v = dis1_t$,此时可以产生 $cnt1[u] \times cnt2[v]$ 的贡献加入 $ans_2$。
2. $u$ 在最短路上,即 $dis1_u + dis2_u = dis1_t$,此时可以产生 $cnt1[v] \times cnt2[u]$ 的贡献加入 $ans_2$。
:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int MOD = 1e9 + 7;int n, m, s, t, dis1[200005], dis2[200005];int cnt1[200005], cnt2[200005];struct node { int id, ju; bool operator < (const node A) const { return ju > A.ju; }};struct B { int u, v;} b[200005];vector<node> g[200005];void bfs (int q, int dis[], int cnt[]) { for (int i = 1; i <= n; i++) dis[i] = -1; queue<int> pq; dis[q] = 0, cnt[q] = 1; pq.push(q); while(!pq.empty()) { int u = pq.front(); pq.pop(); for (int i = 0; i < g[u].size(); i++) { int d = g[u][i].id; if (dis[d] == -1) { dis[d] = dis[u] + 1; cnt[d] = cnt[u]; pq.push(d); } else if (dis[d] == dis[u] + 1) { cnt[d] += cnt[u], cnt[d] %= MOD; } } }}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while(T--) { cin >> n >> m; for (int i = 1; i <= n; i++) { g[i].clear(); } cin >> s >> t; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v; w = 1; b[i] = {u, v}; g[u].push_back({v, w}); g[v].push_back({u, w}); } // memset(dis1, 0x3f, sizeof(dis1)); // memset(dis2, 0x3f, sizeof(dis2)); memset(cnt1, 0, sizeof(cnt1)); memset(cnt2, 0, sizeof(cnt2)); bfs(s, dis1, cnt1); bfs(t, dis2, cnt2); int ans = cnt1[t]; for (int i = 1; i <= m; i++) { int u = b[i].u, v = b[i].v; if (dis1[u] == dis1[v]) { if (dis1[v] + dis2[v] == dis1[t]) ans += cnt1[u] * cnt2[v] % MOD, ans %= MOD; if (dis1[u] + dis2[u] == dis1[t]) ans += cnt1[v] * cnt2[u] % MOD, ans %= MOD; } } cout << ans << "\n"; } return 0;}```:::