Cod sursa(job #1076108)

Utilizator StickmanLazar Alexandru Stickman Data 9 ianuarie 2014 21:46:40
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

vector < long int> v[50002];
long int n,m,cost[50001],x,y,c,a;
long int inf=1<<30;
queue <long int> q;

void bellman()
{
    int a;
    while(!q.empty())
    {
        a=q.front();
        for(int k=0; k<v[a].size(); k+=2)
        {

            if(cost[v[a][k]]>cost[a]+v[a][k+1])
            {
                q.push(v[a][k]);
                cost[v[a][k]]=cost[a]+v[a][k+1];

            }
        }
        q.pop();
    }

}

int main()
{
    ifstream in("dijkstra.in");
    ofstream out("dijkstra.out");
    in>>n>>m;
    for(int i=0; i<m; i++)
    {
        in>>x>>y>>c;
        v[x].push_back(y);
        v[x].push_back(c);

    }
    in.close();
    for(int i=2; i<=n; i++)
    {
        cost[i]=inf;
    }
    q.push(1);
    bellman();
    for(int i=2; i<=n; i++)
        if(cost[i]!=inf)
        out<<cost[i]<<" ";
    else out<<0<<" ";
    out.close();
    return 0;
}