Cod sursa(job #1074899)

Utilizator pulseOvidiu Giorgi pulse Data 8 ianuarie 2014 09:27:22
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 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[10005];
vector <long int> v[10005];
queue <long 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 ();
    fout << (cost[N] < INF ? cost[N] : 0);
    fin.close (); fout.close ();
    return 0;
}