Cod sursa(job #2491629)

Utilizator IATI2019Iati Shumen IATI2019 Data 12 noiembrie 2019 21:19:21
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;
typedef pair <int,int> pii;
priority_queue <pii,vector <pii> ,greater <pii> > pq;
vector <pii> muchii[50001];
int n,m;
int viz[50001];
int dist[50001];
void Dijkstra(){
    int i;
    for(i = 2;i <= n;i++){
        dist[i] = 2e9;
    }
    dist[1] = 0;
    pq.push({0,1});
    while(!pq.empty()){
        int nod = pq.top().second;
        int distanta = pq.top().first;
        pq.pop();
        if(!viz[nod]){
        for(auto x : muchii[nod]){
            if(distanta + x.second < dist[x.first]){
                dist[x.first]  = distanta + x.second;
                pq.push({dist[x.first],x.first});
            }
        }
        viz[nod] = 1;
        }
    }
}
int main()
{

    ifstream cin("dijkstra.in");
    ofstream cout("dijkstra.out");
    int i;
    cin >> n >> m;
    for(i = 1;i <= m;i++){
        int x,y,z;
        cin >> x >> y >> z;
        muchii[x].push_back({y,z});
    }
    Dijkstra();
    for(i = 2;i <= n;i++){
            if(dist[i] != 2e9)
        cout << dist[i] << " ";
    else cout << 0 << " ";
    }
    return 0;
}