Cod sursa(job #1628072)

Utilizator tudorgalatanRoman Tudor tudorgalatan Data 3 martie 2016 20:50:40
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#define InFile  "dijkstra.in"
#define OutFile "dijkstra.out"
#define nmax 50002
#define mmax 250002
#define inf 200000000

using namespace std;

ifstream fin  (InFile);
ofstream fout (OutFile);

struct muchie
{
    long x, y, c;
} G[mmax];

long D[nmax], M, N;
long i, x, y, c, ok;

int main ()
{
    fin >> N >> M;
    for (i=1; i<=M; i++)
    {
        fin >> x >> y >> c;
        G[i].x = x;
        G[i].y = y;
        G[i].c = c;
        if (x == 1)
            D[y] = c;
    }
    for (i=2; i<=N; i++)
        if (D[i] == 0)
            D[i] = inf;
    while (ok == 0)
    {
        ok = 1;
        for (i=1; i<=M; i++)
            if (D[G[i].y]>D[G[i].x]+G[i].c)
        {
            D[G[i].y] = D[G[i].x]+G[i].c;
            ok = 0;
        }
    }
    for (i=2; i<=N; i++)
        if (D[i]!=inf)
            fout << D[i] << ' ';
    else
        fout << 0 << ' ';
    return 0;
}