觉得这个题解写的很好:可以参考一下
题解 P4568 【[JLOI2011]飞行路线】 - Heartlessly 的博客 - 洛谷博客 (luogu.com.cn)
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #define x first #define y second using namespace std; typedef pair<int, int> PII; const int N = 200010, M = 500010 * 6; int S, E; int n, m, k; bool st[N]; int dist[N]; int h[N], e[M], ne[M], w[M], idx; void add(int a, int b, int c) { e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx; idx ++ ; } void dijkstra() { memset(dist, 0x3f, sizeof dist); dist[S] = 0; priority_queue<PII, vector<PII>, greater<PII>> q; q.push({dist[S], S}); while (q.size()) { auto t = q.top(); q.pop(); int ver = t.y; if (st[ver]) continue; st[ver] = true; for (int i = h[ver]; i != -1; i = ne[i]) { int j = e[i]; if (dist[j] > dist[ver] + w[i]) { dist[j] = dist[ver] + w[i]; q.push({dist[j], j}); } } } } int main() { cin >> n >> m >> k >> S >> E; memset(h, -1, sizeof h); for (int i = 0; i < m; i ++ ) { int a, b, c; scanf("%d %d %d", &a, &b, &c); add(a, b, c), add(b, a, c); for (int j = 1; j <= k; j ++ ) { add(a + (j - 1) * n, b + j * n, 0); add(b + (j - 1) * n, a + j * n, 0); add(a + j * n, b + j * n, c); add(b + j * n, a + j * n, c); } } dijkstra(); int ans = 0x3f3f3f3f; for (int i = 0; i <= k; i ++ ) ans = min(ans, dist[E + i * n]); cout << ans << endl; return 0; }
洛谷:P4568 [JLOI2011] 飞行路线
最新推荐文章于 2023-06-06 19:55:25 发布