Cod sursa(job #2203043)

Utilizator hertzalauPOPESCU ION hertzalau Data 10 mai 2018 19:40:18
Problema Algoritmul lui Dijkstra Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m, mat[1005][1005], c[1000000], dist[1005];


int main()
{
    in >> n >> m;
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) mat[i][j] = -1;
    for(int i= 1; i <= m; i++)
    {
        int x, y, z;
        in >> x >> y >> z;
        mat[x][y] = z;
    }
    c[1] = 1;
    int s = 0, ct = 1;
    for(int i = 1; i <= n; i++)
        dist[i] = 999999999;
    dist[1] = 0;
    while(s != ct)
    {
        s++;
        int x = c[s];
        for(int i = 1; i <= n; i++)
            if(mat[x][i] != -1 && dist[x] + mat[x][i] < dist[i])
            {
                ct++;
                c[ct] = i;
                dist[i] = dist[x] + mat[x][i];
            }
    }
    for(int i = 2; i <= n; i++)
        if(dist[i] != 999999999)
        out << dist[i] << " ";
    else out << 0 << " ";
    return 0;
}