Cod sursa(job #1074896)

Utilizator pulseOvidiu Giorgi pulse Data 8 ianuarie 2014 09:24:02
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f

using namespace std;

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

int N, M, cost[2002];
vector <int> v[2002];
queue <int> q;

void bellman ()
{
    q.push (1);
    while (!q.empty ())
    {
        int x = q.front ();
        for (int i = 0; i < v[x].size (); i += 2)
        {
            if (cost[v[x][i]] > cost[x] + v[x][i + 1])
            {
                q.push (v[x][i]);
                cost[v[x][i]] = cost[x] + v[x][i + 1];
            }
        }
        q.pop ();
    }
}

int main ()
{
    fin >> N >> M;
    for (int i = 0, nod1, nod2, c; i < M; ++i)
    {
        fin >> nod1 >> nod2 >> c;
        v[nod1].push_back (nod2);
        v[nod1].push_back (c);
    }
    for (int i = 2; i <= N; ++i)
        cost[i] = INF;
    //q.push (1);
    bellman ();
    for (int i = 2; i <= N; ++i)
        fout << (cost[i] < INF ? cost[i] : 0) << " ";
    //fout << (cost[N] < INF ? cost[N] : 0);
    fin.close (); fout.close ();
    return 0;
}