分析
经典的分层最短路题(我不会)。
建 \(k+1\) 层图,跑一遍最短路,找到 \(dis[i]\) 最小的一个。
代码
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define il inline #define re register #define INF 0x3f3f3f3f #define tie0 cin.tie(0),cout.tie(0) #define fastio ios::sync_with_stdio(false) #define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout) using namespace std; typedef long long ll; template <typename T> inline void read(T &x) { T f = 1; x = 0; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1; for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x *= f; } const int N = 600005; const int M = 2000005; struct node { int id, v; bool operator < (const node &x) const {return v > x.v;} }; struct edge { int to, nxt, val; } e[M*2]; int n, m, k, s, t, ans; int head[M*2], cnt, dis[N]; bool vis[N]; priority_queue <node> q; void insert(int u, int v, int w) { e[++cnt].to = v, e[cnt].val = w, e[cnt].nxt = head[u], head[u] = cnt; } void Dijkstra(int s) { memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; q.push((node){s, 0}); while (!q.empty()) { int u = q.top().id; q.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to, w = e[i].val; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; q.push((node){v, dis[v]}); } } } } int main() { int u, v, w; ans = INF; read(n), read(m), read(k); read(s), read(t); for (int i = 1; i <= m; ++i) { read(u), read(v), read(w); insert(u, v, w); insert(v, u, w); for (int j = 0; j < k; ++j) { insert(u + j * n, v + (j + 1) * n, 0); i#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define il inline #define re register #define INF 0x3f3f3f3f #define tie0 cin.tie(0),cout.tie(0) #define fastio ios::sync_with_stdio(false) #define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout) using namespace std; typedef long long ll; template <typename T> inline void read(T &x) { T f = 1; x = 0; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1; for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x *= f; } const int N = 600005; const int M = 2000005; struct node { int id, v; bool operator < (const node &x) const {return v > x.v;} }; struct edge { int to, nxt, val; } e[M*2]; int n, m, k, s, t, ans; int head[M*2], cnt, dis[N]; bool vis[N]; priority_queue <node> q; void insert(int u, int v, int w) { e[++cnt].to = v, e[cnt].val = w, e[cnt].nxt = head[u], head[u] = cnt; } void Dijkstra(int s) { memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; q.push((node){s, 0}); while (!q.empty()) { int u = q.top().id; q.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to, w = e[i].val; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; q.push((node){v, dis[v]}); } } } } int main() { int u, v, w; ans = INF; read(n), read(m), read(k); read(s), read(t); for (int i = 1; i <= m; ++i) { read(u), read(v), read(w); insert(u, v, w); insert(v, u, w); for (int j = 0; j < k; ++j) { insert(u + j * n, v + (j + 1) * n, 0); insert(v + j * n, u + (j + 1) * n, 0); insert(u + (j + 1) * n, v + (j + 1) * n, w); insert(v + (j + 1) * n, u + (j + 1) * n, w); } } Dijkstra(s); for (int i = 0; i <= k; ++i) ans = min(ans, dis[t + i * n]); printf("%d", ans); return 0; }nsert(v + j * n, u + (j + 1) * n, 0); insert(u + (j + 1) * n, v + (j + 1) * n, w); insert(v + (j + 1) * n, u + (j + 1) * n, w); } } Dijkstra(s); for (int i = 0; i <= k; ++i) ans = min(ans, dis[t + i * n]); printf("%d", ans); return 0; }