Cod sursa(job #2348251)

Utilizator al3xionescuIonescu Alexandru al3xionescu Data 19 februarie 2019 15:58:14
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>
 
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int dist[50005];
bool viz[50005];
vector< pair<int,int> > v[50005];
priority_queue< pair<int,int> > q;
int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=m; i++)
        {
            int a,b,val;
            in >> a >> b >> val;
            v[a].push_back({b,val});
        }
    for (int i = 2; i<=n; i++)
        dist[i] = 1<<30;
    q.push({0,1});
    while (!q.empty())
    {
        int now = q.top().second;
        q.pop();
        if (viz[now])
            continue;
        viz[now] = 1;
        for (auto it: v[now])
        {
            if (it.second+dist[now]<dist[it.first])
                {
                    dist[it.first] = it.second+dist[now];
                    q.push({-dist[it.first],it.first});
                }
        }
    }
    for (int i = 2; i<=n; i++)
        if (dist[i] == 1<<30)
            out << "0 ";
        else
            out << dist[i] << " ";
}