Cod sursa(job #3246201)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 2 octombrie 2024 11:29:40
Problema Algoritmul lui Dijkstra Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <climits>
#include <vector>

using namespace std;

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

const int nmax = 50005;
const int inf = INT_MAX;

struct ceva{
    int vec, cost;
};

int n, m, fr[nmax], a[nmax];
vector<ceva> v[nmax];

void dijk(int nod)
{
    a[nod] = 0;
    for(int i = 1; i <= n; i ++)
    {
        int mini = inf, poz;
        for(int j = 1; j <= n; j ++)
            if(!fr[j] && mini > a[j])
                mini = a[j], poz = j;

        if(mini == inf)
            break;

        fr[poz] = 1;

        for(auto x : v[poz])
            if(a[x.vec] > x.cost + a[poz])
                a[x.vec] = x.cost + a[poz];
    }
}

int main()
{
    f >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        int x, y, c;
        f >> x >> y >> c;
        v[x].push_back({y, c});
    }

    for(int i = 1; i <= n; i ++)
        a[i] = inf;

    dijk(1);

    for(int i = 2; i <= n; i ++)
        if(a[i] != inf)
            g << a[i] << " ";
        else
            g << 0 << " ";
    return 0;
}