求助

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

SuperCowHorse @ 2022-03-12 18:29:54

#include<bits/stdc++.h>
using namespace std;
struct node{
    int pos,dis;
    bool operator <(const node &a) const{return a.dis<dis;}
}tmp;
priority_queue<node>q;
int n,m,u,v,w,s,cnt;
int head[10005],dis[10005];
bool used[10005];
struct Node{
    int u,v,w,next;
}e[500005];
void add(int u,int v,int w)
{
    ++cnt;
    e[cnt].u=u;
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}
int main()
{
    scanf("%d %d %d",&n,&m,&s);
    while(m--)
    {
        scanf("%d %d %d",&u,&v,&w);
        add(u,v,w);
        add(v,u,w);
    }
    memset(dis,63,sizeof(dis));
    dis[s]=0;
    q.push((node){s,0});
    while(!q.empty())
    {
        tmp=q.top();q.pop();u=tmp.pos;
        if(used[u]) continue;
        used[u]=1;
        for(int i=head[u];i;i=e[i].next)
        {
            v=e[i].v;
            if(dis[v]>dis[u]+e[i].w)
            {
                dis[v]=dis[u]+e[i].w;
                if(!used[v]) q.push((node){v,dis[v]});
            }
        }
    }
    printf("%d",dis[1]);
    for(int i=2;i<=n;i++)
        printf(" %d",dis[i]);
}

by j1ANGFeng @ 2022-03-12 18:31:25

@chenye3

是有向边


by lsj2009 @ 2022-03-12 18:36:27

@chenye3 大根堆没插相反数。


|