Cod sursa(job #2932698)

Utilizator bogdan.schiopBogdan Schiop bogdan.schiop Data 3 noiembrie 2022 18:33:06
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>

#define inf 2147483647

using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

int a[3002][3002];
int aux, aux2, c;
int n, m;
int distante[3002];

void citire()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> aux >> aux2 >> c;
        a[aux][aux2] = c;
        if(aux == 1) distante[aux2] = c;
    }
    for(int i = 2; i <= n; i++)
        {
            if(distante[i] == 0)
                distante[i] = inf;
        }
}

void dijkstra()
{
    bool ok = true;
    while(ok)
    {
        ok = false;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(distante[i] + a[i][j] < distante[j] && a[i][j])
                {
                    distante[j] = distante[i] + a[i][j];
                    ok = true;
                }
            }
        }
    }
}

int main()
{
    citire();
    dijkstra();
    for(int i = 2; i <= n; i++)
    {
        if(distante[i] != inf)
            fout << distante[i] << ' ';
        else
            fout << 0 <<' ';
    }
    return 0;
}