Cod sursa(job #1912322)

Utilizator savigunFeleaga Dragos-George savigun Data 8 martie 2017 00:59:52
Problema Algoritmul lui Dijkstra Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

struct Edge {
    int cost;
    int node;
};
class Compare{
public:
    bool operator() (Edge a, Edge b) {
        return a.cost > b.cost;
    }
};

int n, m, d[50001];
bool viz[50001];
vector<Edge> g[50001];
const int INF = 1e9 * 2 + 1;
//priority_queue<Edge, vector<Edge>, Compare> q;
queue<Edge> q;

void citire();


int main()
{
    citire();
    Edge e;
    e.cost = 0;
    e.node = 1;
    q.push(e);

    while (!q.empty()) {
        //e = q.top();
        e = q.front();
        q.pop();
        int x = e.node;
        //if (viz[x]) continue;
        //viz[x] = true;
        for (int i = 0; i < g[x].size(); ++i) {
            int y = g[x][i].node;
            //if (!viz[y]) {
                if (e.cost + g[x][i].cost < d[y]) {
                    d[y] = e.cost + g[x][i].cost;
                    Edge edge; edge.cost = d[y]; edge.node = y;
                    q.push(edge);
                }
            //}
        }
    }

    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;
        Edge e;
        e.node = y;
        e.cost = cost;
        g[x].push_back(e);
    }

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

    in.close();
}