Cod sursa(job #1827191)

Utilizator valentinoMoldovan Rares valentino Data 11 decembrie 2016 15:47:12
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <vector>
#include <queue>
#define NM 50005
#define INF 1000000000
using namespace std;

vector < pair < int, int > >v[NM];
queue < int > q;

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

int n, m, cost[NM], l[NM];

void Dijkstra()
{
    int nod2;
    for(int i = 1; i <= n; ++i)
        cost[i] = INF;
    q.push(1);
    cost[1] = 0;
    while(q.empty() == 0)
    {
        nod2 = q.front();
        q.pop();
        for(int i = 0 ; i < l[nod2]; ++i)
        {
            if(cost[v[nod2][i].first] > cost[nod2] + v[nod2][i].second)
            {
                cost[v[nod2][i].first] = cost[nod2] + v[nod2][i].second;
                q.push(v[nod2][i].first);
            }
        }
    }
    for(int i = 2; i <= n; ++i)
        {
            if(cost[i] != INF) g << cost[i] << ' ';
            else g << "0 ";
        }


}

int main()
{
    int x,y,c;
    f >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        f >> x >> y >> c;
        l[x]++;
        v[x].push_back(make_pair(y,c));
    }
    Dijkstra();
}