Cod sursa(job #3284934)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 12 martie 2025 13:04:11
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#include <vector>
#include <queue>
#define inf 2e9
#define nmax 100001
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n,m,x,y,c,dp[nmax];
vector<pair<int,int>>v[nmax];
bool viz[nmax];
struct cmp{
    bool operator()(const pair<int,int>& a,const pair<int,int>& b){
        return a.second>b.second;
    }
};
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp>q;
void dijkstra(){
    for(int i=2;i<=n;i++)
        dp[i]=inf;
    q.push({1,0});
    while(!q.empty()){
        int nod=q.top().first;
        q.pop();
        if(viz[nod])
            continue;
        viz[nod]=1;
        for(auto i:v[nod])
            if(!viz[i.first]&&dp[i.first]>dp[nod]+i.second){
                dp[i.first]=dp[nod]+i.second;
                q.push({i.first,dp[i.first]});
            }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>x>>y>>c;
        v[x].push_back({y,c});
    }
    dijkstra();
    for(int i=2;i<=n;i++)
        if(dp[i]==inf)
            cout<<"0 ";
        else
            cout<<dp[i]<<" ";
    return 0;
}