Cod sursa(job #2534305)

Utilizator RaresLiscanLiscan Rares RaresLiscan Data 30 ianuarie 2020 13:07:06
Problema Algoritmul lui Dijkstra Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

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

int v[50005];

struct varf
{
    int nod, cost;
    bool operator < (const varf & b) const {
        return cost < b.cost;
    }
};

vector <varf> g[50005];
priority_queue <varf> q;

void dijkstra()
{
    q.push({1, 0});
    v[1] = 0;
    while (!q.empty()) {
        int x = q.top().nod;
        int y = q.top().cost;
        q.pop();
        if (v[x] != y) continue;
        for (auto i : g[x]) {
            int cn = i.nod;
            int cc = i.cost;
            if (v[cn] > y + cc) {
                v[cn] = y + cc;
                q.push({cn, v[cn]});
            }
        }
    }
}

int main()
{
    int n, m;
    fin >> n >> m;
    for (int i = 1; i <= m; i ++) {
        int a, b, c;
        fin >> a >> b >> c;
        g[a].push_back({b, c});
    }
    memset(v, INF, sizeof(v));
    dijkstra();
    for (int i = 2; i <= n; i ++) {
        if (v[i] >= INF) fout << 0 << " ";
        else fout << v[i] << " ";
    }
    return 0;
}