Cod sursa(job #1976934)

Utilizator blatulInstitutul de Arta Gastronomica blatul Data 4 mai 2017 16:27:48
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
using namespace std;

#define NMAX 50002

FILE *f = fopen("dijkstra.in", "r");
FILE *g = fopen("dijkstra.out", "w");

vector< pair<int, int> > graf[NMAX];

int dist[NMAX];

void Dijkstra(const int N, const int start) {

    priority_queue< pair<int, int> > Heap;

    for (int i = 1; i <= N; ++i)
        dist[i] = INT_MAX;

    dist[start] = 0;

    Heap.push(make_pair(-dist[start], start));

    while (!Heap.empty()) {

        int nod = Heap.top().second;
        Heap.pop();

        for (auto adj: graf[nod]) {

            if (dist[adj.first] > dist[nod] + adj.second) {

                dist[adj.first] = dist[nod] + adj.second;
                Heap.push(make_pair(-dist[adj.first], adj.first));
            }
        }
    }
}


int main() {

    int N, M;

    fscanf(f, "%d%d", &N, &M);

    int x, y, c;
    while (M--) {

        fscanf(f, "%d%d%d", &x, &y, &c);

        graf[x].push_back(make_pair(y, c));
    }

    Dijkstra(N, 1);

    for (int i = 2; i <= N; ++i)
        fprintf (g, "%d ", (dist[i] == INT_MAX ? 0 : dist[i]));

    return 0;
}