堆优板子寄了,求助

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

lyhqwq @ 2023-03-03 22:26:11

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
const int M=200005;
const int INF=2147483647;
struct edge{
    int v;
    int nxt;
    int w; 
}edge[M<<1];
struct node{
    int x,dis;
    bool operator <(const node &rhs)const{
        dis>rhs.dis;
    }
};
int head[N],cnt;
int n,m,s;
int dis[N],vis[N];
priority_queue<node> q;
void addedge(int u,int v,int w){
    edge[++cnt].v=v,edge[cnt].w=w,edge[cnt].nxt=head[u],head[u]=cnt;
}
void dijkstra(int s){
    for(int i=1;i<=n;i++) dis[i]=INF;
    dis[s]=0;
    q.push((node){s,dis[s]});
    while(!q.empty()){
        int u=q.top().x;
        q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=head[u];i;i=edge[i].nxt){
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].w){
                dis[v]=dis[u]+edge[i].w;
                q.push((node){v,dis[v]});
            }
        }
    }
}
int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    scanf("%d%d%d",&n,&m,&s);
    for(int i=1;i<=m;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w);
    }
    dijkstra(s);
    for(int i=1;i<=n;i++){
        printf("%d ",dis[i]);
    }
    return 0;
}

by Dream_weavers @ 2023-03-03 22:28:42

开longlong


|