Cod sursa(job #2576769)

Utilizator sipdavSipos David Oliver sipdav Data 6 martie 2020 22:32:05
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

const int dim = 50010;

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

int n, m, dist[dim];
vector<pair<int, int> > muchii[dim];

struct cmp
{
    bool operator() (const int& a, const int& b) const
    {
        return dist[a] < dist[b];
    }
};

int main()
{
    in>>n>>m;
    int x, y, c;
    for(int i = 1; i <= m; i++)
    {
        in>>x>>y>>c;
        muchii[x].push_back({y, c});
    }
    vector<pair<int, int> >::iterator it;
    set<int> s;
    int nod;
    dist[1] = 0;
    s.insert(1);
    while(!s.empty())
    {
        nod = *(s.begin());
        s.erase(s.begin());
        for(it = muchii[nod].begin(); it != muchii[nod].end();it++)
        {
            if(dist[it->first] > dist[nod] + it->second || dist[it->first] == 0)
            {
                dist[it->first] = dist[nod] + it->second;
                s.insert(it->first);
            }
        }
    }
    for(int i = 2; i <= n; i++)
        out<<dist[i]<<' ';
    return 0;
}