60分,求助,玄关
  • 板块P1608 路径统计
  • 楼主TAMIYA
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/6/3 17:20
  • 上次更新2024/6/3 20:14:55
查看原帖
60分,求助,玄关
1252040
TAMIYA楼主2024/6/3 17:20
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 2010, M = 4000010 << 1, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], w[M], idx = 0;
int dist[N], cnt[N];
bool st[N];
int n, m;

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void dijkstra() {
    fill(dist, dist + N, INF);
    fill(cnt, cnt + N, 0);
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});
    dist[1] = 0;
    cnt[1] = 1;

    while (!heap.empty()) {
        auto [d, t] = heap.top();
        heap.pop();

        if (st[t]) continue;
        st[t] = true;

        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (dist[j] > dist[t] + w[i]) {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t];
                heap.push({dist[j], j});
            } else if (dist[j] == dist[t] + w[i]) {
                cnt[j] += cnt[t];
            }
        }
    }
}

int main() {
    memset(h, -1, sizeof h);

    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        add(x, y, z);
    }

    dijkstra();
    if (dist[n] == INF) {
        printf("No answer");
    } else {
        printf("%d %d", dist[n], cnt[n]);
    }
    return 0;
}
2024/6/3 17:20
加载中...