Cod sursa(job #1128738)

Utilizator muresan_bogdanMuresan Bogdan muresan_bogdan Data 27 februarie 2014 18:29:20
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;

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

const int INF = 1000000000;

struct muchie {
    int cost;
    int b;
};

struct nod {
    int cost;
    bool vizitat;
    vector<muchie> vecini;
};

int n, m, i, x, y, c;
nod g[50001];
priority_queue<muchie> coada;

bool operator < (const muchie &a, const muchie &b) {
    return a.cost > b.cost;
}

void dij(muchie x) {
    for(int j = 0; j < g[x.b].vecini.size(); j++) {
        int vecin = g[x.b].vecini[j].b;
        if (!g[vecin].vizitat) {
            coada.push({g[x.b].cost + g[x.b].vecini[j].cost, vecin});
        }
    }
}

int main() {
    fin >> n >> m;
    for(i = 0; i < m; i++) {
        fin >> x >> y >> c;
        g[x].vecini.push_back({c, y});
    }
    coada.push({0, 1});
    while(!coada.empty()) {
        muchie aux = coada.top();
        coada.pop();
        if(g[aux.b].vizitat == false) {
            g[aux.b].cost = aux.cost;
            g[aux.b].vizitat = true;
            dij(aux);
        }
    }
    for(i = 2; i <= n; i++) {
        fout << g[i].cost << ' ';
    }
    fin.close();
    fout.close();
    return 0;
}