Cod sursa(job #1976634)

Utilizator VladGhetinaVlad Ghetina VladGhetina Data 3 mai 2017 21:51:15
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

#define IN "dijkstra.in"
#define OUT "dijkstra.out"
#define l 104*10
#define pb push_back

ifstream fin(IN);
ofstream fout(OUT);

int x , y , v , n , p , m;
int cost[50003];
bool viz[50003];

vector<int>G[50003];
vector<int>C[50003];
queue<int>Q;

void Read()
{
    int i;

    fin >> n >> p;

    for ( i = 1 ; i <= n ; i ++ ){
            fin >> x >> y >> v;

            G[x].pb(y);
            C[x].pb(v);
    }

}

void Solve()
{
    int i;

    cost[p] = 0;
    viz[p] = 1;

    Q.push(p);

    while (!Q.empty())
    {
        x = Q.front();

        for ( i = 0 ; i < G[x].size() ; i ++ )
            if ( viz[G[x][i]] == 0 or cost[G[x][i]] > cost[x] + C[x][i] )
        {
            cost[G[x][i]] = cost[x]+C[x][i];
            Q.push(G[x][i]);
            viz[G[x][i]] = 1;
        }

        Q.pop();
    }

    for ( i = 1 ; i <= n ; i ++ )
        if ( i!= p && cost[i] == 0 )
        fout << cost[i]-1 << " ";
        else fout << cost[i] << " ";
}

int main()
{

    Read();
    Solve();
}