优先队列优化求调

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

MoGuYun_12 @ 2023-11-24 23:58:20

#include <bits/stdc++.h>

using namespace std;
int n,m,s;
int dis[100001];
vector<int> e[100001];
void dij()
{
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
    memset(dis,0x3f,sizeof dis);
    dis[s]=0;
    q.push({dis[s],s});
    while(!q.empty())
    {
        int x=q.top().second;
        q.pop();
        for(int i=0;i<e[x].size();i+=2)
        {
            int y=e[x][i],w=e[x][i+1];
            if(dis[y]>dis[x]+w)
            {
                dis[y]=dis[x]+w;
                q.push({dis[y],y});
            }
        }
    }
}
int main()
{
    cin>>n>>m>>s;
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        e[u].push_back(v),e[u].push_back(w);
        e[v].push_back(u),e[v].push_back(w);
    }
    dij();
    for(int i=1;i<=n;i++)
        cout<<dis[i]<<' ';
    return 0;
}

by MoGuYun_12 @ 2023-11-25 00:00:52

1,4WA 2,3,6T


by __zhy__ @ 2023-11-25 00:03:16

@MoGuYun_12 没有判断是否重复入队


by Argvchs @ 2023-11-25 00:25:36

@MoGuYun_12 有向边


by HPY_xiaxii @ 2023-11-25 18:32:57

太卷力


by MoGuYun_12 @ 2023-11-26 12:43:50

@Argvchs 谢谢


by MoGuYun_12 @ 2023-11-26 12:44:39

@zhenghaoyi 谢谢


|