求助

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

starlight0798 @ 2023-01-20 20:56:11

一直卡在WA AC AC WA AC AC

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define rint register int

const int MAXN = 1e5 + 5;
const int INF = 0x3FFFFFFF;
bool vis[MAXN];
int dis[MAXN];

struct Node{
    int num; int len;
    Node(rint n = 0, rint w = 0) { num = n; len = w; }
    bool operator<(const Node& a) const { return dis[num] > dis[a.num]; }
};

vector<Node> G[MAXN];

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

void dijkstra(rint n, rint s){
    priority_queue<Node> que;
    for (rint i = 1; i <= n; i++) dis[i] = INF;
    dis[s] = 0;
    que.push(Node(s));
    while (!que.empty()){
        rint cur = que.top().num;
        que.pop();
        if (vis[cur]) continue;
        vis[cur] = true;
        for (Node& node : G[cur]){
            rint to = node.num, w = node.len;
            if (dis[to] > dis[cur] + w){
                dis[to] = dis[cur] + w;
                if(!vis[to]) que.push(node);
            }
        }
    }
}

int main()
{
    rint n, m, s, u, v, w;
    n = read(); m = read(); s = read();
    for (rint i = 0; i < m; i++){
        u = read(); v = read(); w = read();
        G[u].push_back(Node(v, w));
    }
    dijkstra(n, s);
    for (rint i = 1; i <= n; i++) cout << dis[i] << " ";
    return 0;
}

by 大眼仔Happy @ 2023-01-20 21:48:10

@starlight0798

一般不是node另存一个dis的吗,怎么到你这里就直接调用数组了呢?


by starlight0798 @ 2023-01-21 00:05:31

@大眼仔Happy 一般不是把dis数组放在全局变量吗?


|