求助dij,悬赏4个关注
查看原帖
求助dij,悬赏4个关注
271803
over_caykl楼主2023/6/27 19:21
#include<bits/stdc++.h>
#define PII pair<LL,int>
using namespace std;
const int N=2e5+5;
typedef long long LL;

int fst[N],nxt[N],t[N],v[N],tot;
void Add_edge(int a,int b,int c){
    nxt[++tot]=fst[a];
    fst[a]=tot;
    t[tot]=b;
    v[tot]=c;
}

int n,m,s;
LL Dis[N];
priority_queue<PII,vector<PII >,greater<PII> > Q;
bool inQ[N];
void Dij(){
    for(int i=1;i<=n;i++) Dis[i]=2147483647ll;
    Dis[s]=0ll;
    Q.push({Dis[s],s}); 
    inQ[s]=1;

    while(!Q.empty()){
        auto T=Q.top(); Q.pop();
        LL l=T.first; int r=T.second;

        for(int i=fst[r];i;i=nxt[i])
            if(Dis[t[i]]> l+v[i]){
                Dis[t[i]]= l+v[i];
                if(!inQ[t[i]]){
                    Q.push({Dis[t[i]],t[i]});
                    inQ[t[i]]=1;
                }
            }
    }
}
int A,B,C;
int main(){
    scanf("%d%d%d",&n,&m,&s);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&A,&B,&C);
        Add_edge(A,B,C);
    }
    Dij();
    for(int i=1;i<=n;i++)
        printf("%lld ",Dis[i]);
    return 0;
}

只AC了第5个点

2023/6/27 19:21
加载中...