Cod sursa(job #1096107)

Utilizator paul_danutDandelion paul_danut Data 1 februarie 2014 15:43:54
Problema Algoritmul Bellman-Ford Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

struct muchie{int y,c;}el;

vector<muchie> a[50001];
vector<muchie>::iterator it;

int x,n,m,i,D[50001];
struct cmp{
     bool operator()(int x,int y)
       {return D[x]<D[y];}
};

#define INF 99999999


priority_queue<int,vector<int>,cmp> qh;

int main()
{
    f>>n>>m;
    for(i=1;i<=m;i++)
        {f>>x>>el.y>>el.c;
        a[x].push_back(el);}

    D[1]=0;
    for(i=2;i<=n;i++)
        D[i]=INF;
    qh.push(1);

    while(!qh.empty())
    {
        x=qh.top();qh.pop();
        for(it=a[x].begin();it!=a[x].end();++it)
            if( D[it->y]> D[x] + it->c )
                {
                D[it->y]= D[x] + it->c;
                qh.push(it->y);
                }
    }

    for(i=2;i<=n;i++)
           g<<D[i]<<' ';

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