Cod sursa(job #1906877)

Utilizator savigunFeleaga Dragos-George savigun Data 6 martie 2017 16:48:04
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

typedef pair<int, int> edge;

int n, m, d[50001];
bool viz[50001];
vector<edge> g[50001];
priority_queue<edge, vector<edge>, greater<edge> > q;
const int INF = 2e9 + 1;

void citire();


int main()
{
    citire();
    q.push(make_pair(0, 1));

    while (!q.empty()) {
        int x = q.top().second;
        int cost = q.top().first;
        q.pop();
        if (viz[x]) continue;

        viz[x] = true;
        for (int i = 0; i < g[x].size(); ++i) {
            int y = g[x][i].second;
            //if (!viz[y]) {
                if (cost + g[x][i].first < d[y]) {
                    d[y] = cost + g[x][i].first;
                    q.push(make_pair(d[y], y));
                }
            //}
        }
    }

    for (int i = 2; i <= n; ++i) {
        if (d[i] == INF) {
            out << 0 << " ";
        } else {
            out << d[i] << " ";
        }
    }

    return 0;
}


void citire() {
    in >> n >> m;
    for (int i = 1, x, y, cost; i <= m; ++i) {
        in >> x >> y >> cost;
        g[x].push_back(make_pair(cost, y));
    }

    for (int i = 1; i <= n; ++i)
        d[i] = INF;

    in.close();
}