Cod sursa(job #3224749)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 16 aprilie 2024 09:08:44
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#include <climits>
#include <vector>

using namespace std;

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

const long long nmax = 50005;
const long long inf = LONG_MAX;
long long fr[nmax], n, m, cost[nmax];
vector< pair<long long, long long> > a[nmax];

void Dij(long long nod)
{
    fr[nod] = 1;
    while(a[nod].size())
    {
        pair<long long, long long> 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(long long i = 1; i <= n; i ++)
    {
        long long x, y, c;
        f >> x >> y >> c;
        a[x].push_back({y, c});
    }

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

    Dij(1);

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

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