Cod sursa(job #3325393)

Utilizator loghin_antoniaLoghin Antonia loghin_antonia Data 25 noiembrie 2025 13:21:24
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb

#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define oo 1e9
struct vd {
    int vf, dist;
    bool operator <(const vd&x) const
    {
        return dist<=x.dist;
    }
};
vector<vd>M[50005];
int D[50005], T[50005];
priority_queue <vd> Q;
int n,m,S;

int main()
{
    
    fin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,d;
        fin>>x>>y>>d;
        M[x].push_back({y,d});
    }
    for(int i=1;i<=n;i++)
        D[i]=oo;
    S=1;
    D[S]=0;
    T[S]=S;
    Q.push({S,0});
    while(!Q.empty())
    {
        int x=Q.top().vf;
        Q.pop();
        for(int i=0;i<M[x].size();i++)
            {
                int y=M[x][i].vf, d=M[x][i].dist;
                if(D[y]>D[x]+d)
                {
                    T[y]=x;
                    D[y]=D[x]+d;
                    Q.push({y,D[y]});
                }
            }
    }
    for(int i=2;i<=n;i++)
        fout<<D[i]<<" ";
    return 0;
}