Cod sursa(job #1918512)

Utilizator valentinoMoldovan Rares valentino Data 9 martie 2017 15:45:28
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;

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

vector < pair < int, int > > G[ 50005 ];
int t, n, m,  cost[ 50005 ], viz[ 50005 ];
queue < int > q;

int main()
{
    int x, y, c, nod, nod2, costcurent, sizec;
    f >> n >> m;
    for(int i = 1; i <= n; ++i) cost[ i ] = inf;
    for(int i = 1; i <= m ;++i)
    {
        f >> x >> y >> c;
        G[ x ].push_back(make_pair(y, c));
    }
    q.push(1);
    cost[ 1 ] = 0;
    while(!q.empty())
    {
        nod = q.front();
        q.pop();
        viz[ nod ] = 0;
        sizec = G[ nod ].size();
        for(int i = 0; i < sizec; ++i)
        {
            costcurent = G[ nod ][ i ].second;
            nod2 = G[ nod ][ i ].first;
            if(cost[ nod2 ] > cost[ nod ] + costcurent)
            {
                cost[ nod2 ] = cost[ nod ] + costcurent;
                if(!viz[ nod2 ])
                {
                    q.push( nod2 );
                    viz[ nod2 ] = 1;
                }
            }
        }
    }
    for(int i = 2; i <= n; ++i)
    {
        if(cost[ i ] != inf) g << cost[ i ] << ' ';
        else g << "0" << ' ';
    }
    return 0;
}