Cod sursa(job #719824)

Utilizator vendettaSalajan Razvan vendetta Data 22 martie 2012 09:21:21
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <set>
#include <vector>
#define nmax 50005

using namespace std;

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

int n, m, d[nmax];
vector<pair<int,int> > gf[nmax];
set<pair<int,int> > h;

void citeste(){

    f >> n >> m;
    for(int i=1; i<=m; i++){
        int x, y, c;
        f >> x >> y >> c;
        gf[x].push_back(make_pair(y,c));
    }

}

void dijkstra(){

    for(int i=0; i<nmax; i++) d[i] = (1<<30);//nu ma duc pana la nmax !!

    d[1] = 0;

    h.insert(make_pair(0,1));

    for(; h.size(); ){
        int nod = (*h.begin()).second;
        int Min = (*h.begin()).first;
        h.erase(*h.begin());
        for(int i=0; i<gf[nod].size(); i++){
            int vc = gf[nod][i].first;
            int cost = gf[nod][i].second;
            if (d[vc] > d[nod] + cost){
                d[vc] = d[nod] + cost;
                h.insert(make_pair(d[vc], vc));
            }
        }
    }

}

void scrie(){

    for(int i=2; i<=n; i++)
        if ( d[i] == (1<<30)) g << "0" << " ";
            else g << d[i] << " ";

}

int main(){

    citeste();
    dijkstra();
    scrie();

    f.close();
    g.close();

    return 0;

}