RE求调
查看原帖
RE求调
1157431
Houjiadong楼主2024/3/14 18:55
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 5e5+ 10;
int n, m, k;
int s, q;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c = 0) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[s] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({ 0,s });
    while (heap.size()) {
        auto t = heap.top();
        heap.pop();
        int ver = t.second, distance = t.first;
        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];
                heap.push({ dist[j], j });
            }
        }

    }

}
int main() {
    cin >> n >> m >> k;
    cin >> s >> q;
    memset(h, -1, sizeof h);
    while (m--) {
        int a, b, c;
        cin >> 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);
            add(b + (j - 1) * n, a + j * n);
            add(a + j * n, b, c);
            add(b + j * n, a, c);
        }
    }
    dijkstra();
    cout << dist[q];
}

菜狗不易qwq

2024/3/14 18:55
加载中...