Cod sursa(job #2365984)

Utilizator qwerty1234qwerty qwerty1234 Data 4 martie 2019 17:51:22
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb

#include <bits/stdc++.h>


using namespace std;

ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");

const int Nmax = 50005;

struct G
{
    int node , val;
    bool operator < (const G &e) const
    {
        return val > e.val;
    }
};


priority_queue < G > Q;

int n , m , dist[Nmax];

vector < pair < int , int > > L[Nmax];

bool viz[Nmax];

int main()
{
    int x , y , c;
    G w;
    fin >> n >> m;
    for(int i = 1 ; i <= n ; i++)
    {
        fin >> x >> y >> c;
        L[x].push_back({y , c});
        L[y].push_back({x , c});
    }
    for(int i = 1 ; i <= n ; i++)
        dist[i] = 1e9;
    dist[1] = 0;
    Q.push({1 , 0});
    while(!Q.empty())
    {
        w = Q.top();
        Q.pop();
        if(!viz[w.node])
        {
            viz[w.node] = true;
             for(auto it : L[w.node])
                    if(dist[it.first] > dist[w.node] + it.second)
                    {
                        dist[it.first] = dist[w.node] + it.second;
                        Q.push({it.first , dist[it.first]});
                    }
        }

    }
    for(int i = 2 ; i <= n ; i++)
        if(dist[i] == 1e9)
            fout << "0 ";
        else fout << dist[i] << " ";
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}