Cod sursa(job #3224748)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 16 aprilie 2024 09:06:50
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 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;
int fr[nmax], n, m, cost[nmax];
vector< pair<int, int> > a[nmax];

void Dij(int nod)
{
    fr[nod] = 1;
    while(a[nod].size())
    {
        pair<int, int> k = a[nod].back();
        a[nod].pop_back();
        cost[k.first] = min(cost[k.first], k.second + cost[nod]);
        if(!fr[k.first])
            Dij(k.first);
    }
}

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

    for(int i = 2; i <= n; i ++)
        cost[i] = inf;

    Dij(1);

    for(int i = 2; i <= n; i ++)
        if(cost[i] == inf)
            g << 0 << " ";

        else
            g << cost[i] << " ";
    return 0;
}