Dijkstra 84pts 求调

P4779 【模板】单源最短路径(标准版)

Epi4any @ 2022-10-28 17:36:02

【WA on test #5】

#include <iostream>
#include <cstdio>
#include <bitset>
#include <queue>
#include <cstring>
using namespace std;

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}

const int maxn = 100005;
const int maxm = 200005;
const int INF = 0x7ffffff;

int n, m, s;
int cnt, head[maxm], dis[maxm], vis[maxm];

struct Edge {
    int v, w, next;
} edge[maxm];

struct node {
    int dis, ind;
    bool operator > (const node &x)const {
        return dis > x.dis;
    }
};

priority_queue<node, vector<node>, greater<node> > q;

inline void addedge(int u, int v, int w) {
    edge[++cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt;
}

void dijkstra(int st) {
    for (int i = 1; i <= n; i++) dis[i] = INF;
    dis[st] = 0;
    q.push((node) {
        0, st
    });
    while (!q.empty()) {
        int cur = q.top().ind;
        q.pop();
        if (vis[cur]) continue;
        vis[cur]=1;
        for (int j = head[cur]; j; j = edge[j].next) {
            if (dis[edge[j].v] > dis[cur] + edge[j].w) {
                dis[edge[j].v] = dis[cur] + edge[j].w;
                if (vis[edge[j].v] == 0) q.push((node) {
                    dis[edge[j].v], edge[j].v
                });
            }
        }
    }
}

int main() {
    n = read(), m = read(), s = read();
    for (int i = 1, u, v, w; i <= m; i++) {
        u = read(), v = read(), w = read();
        addedge(u, v, w);
    }
    dijkstra(s);
    for (int i = 1; i <= n; i++) {
        if (dis[i] != INF) cout << dis[i] << " ";
        else cout << (1 << 37) - 1 << endl;
    }
    cout << endl;
    return 0;
}

大佬轻喷


by wycha @ 2022-10-28 17:42:15

有没有一种可能,0x7ffffff没有1e9


by wycha @ 2022-10-28 17:43:17

@Faith_toChange


by Kevin_Mamba @ 2022-10-28 17:44:52


by Epi4any @ 2022-10-28 18:09:02

@2124Kobe @wycha 谢谢!


|