Cod sursa(job #1839468)

Utilizator alexandruchiriacAlexandru Chiriac alexandruchiriac Data 2 ianuarie 2017 22:25:31
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

#define nrmax 50001
#define inf 288888888
#define nod first
#define cost second
#define pb push_back
#define mp make_pair
using namespace std;

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

int d[nrmax];
bool used[nrmax];
int n , m , x;
vector < pair < int , int > >G[nrmax];
queue < int >Q;

void bellman_ford ( int x )
{
    memset(d,inf,sizeof d);
    d[x] = 0;
    used[x] = 1;
    Q.push(x);
    while ( !Q.empty() )
    {
        int y = Q.front();
        Q.pop();
        used[y] = 0;
        for ( int i = 0 ; i < G[y].size() ; i++ )
        {
            if ( d[ G[y][i].nod ] > d[y] + G[y][i].cost )
            {
               d[ G[y][i].nod ] = d[y] + G[y][i].cost;
               if ( !used [ G[y][i].nod ] )
               {
                   used[ G[y][i].nod ] = 1;
                   Q.push( G[y][i].nod );
               }
            }
        }
    }
    for ( int i = 2; i <= n ; i++ )
    {
        if ( d[i] >= inf ) d[i] = 0;
        g << d[i] << " " ;
    }
}

int main()
{
    f >> n >> m;
    int a,b,c;
    while ( m )
    {
        f >> a >> b >> c;
        G[a].pb(mp(b,c));
        m--;
    }
    x = 1;
    bellman_ford(x);
    return 0;
}