WA+RE,求调(玄关)

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

Guizy @ 2023-10-20 20:03:46

#include<bits/stdc++.h>
#define ll long long
#define Max 200001
using namespace std;

const ll inf=INT_MAX;
ll n,m,s,cnt;
ll dis[Max],nxt[Max],val[Max];
ll to[Max],h[Max],vis[Max];

struct node{
    ll v,w;
    friend bool operator<(node a,node b){
        return a.w>b.w;
    }
};

priority_queue<node>q;

void dijkstra(){

    for(ll i=0;i<=n;i++) dis[i]=inf;

    dis[s]=0;
    q.push({s,0});

    while(!q.empty()){
        ll u=q.top().v; q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(ll i=h[u];i;i=nxt[i]){
            if(dis[to[i]]>dis[u]+val[i]){
                dis[to[i]]=dis[u]+val[i];
                q.push({dis[to[i]],to[i]});
            }
        }
    }
}

void add(ll a,ll b,ll c){
    to[++cnt]=b;
    val[cnt]=c;
    nxt[cnt]=h[a];
    h[a]=cnt;
}

signed main(){

//  freopen("test.in","r",stdin);
//  freopen("test.out","w",stdout);

    scanf("%lld%lld%lld",&n,&m,&s);
    ll u,v,w;

    for(ll i=1;i<=m;i++){
        scanf("%lld%lld%lld",&u,&v,&w);
        add(u,v,w);
    }

    dijkstra();

    for(ll i=1;i<=n;i++){
        printf("%lld ",dis[i]);
    }

    return 0;
}

by ryf_loser @ 2023-10-20 20:06:09

@Guizy q.push({dis[to[i]],to[i]});

改成 q.push({to[i],dis[to[i]]});


by aoeiuv @ 2023-10-20 20:06:18

数组开小了,边数要乘以2


by TryHardToBeAlive @ 2023-10-20 20:09:35

数组边数没问题的吧200001就是m的2e5啊


by Guizy @ 2023-10-20 20:11:08

@ryf20100124 thanks


by Guizy @ 2023-10-20 20:12:08

已关


|