Cod sursa(job #1067538)

Utilizator tudorv96Tudor Varan tudorv96 Data 26 decembrie 2013 23:01:01
Problema Sate Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.02 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

#define in "sate.in"
#define out "sate.out"
#define N 30005
#define pb push_back
#define y first
#define c second

typedef pair <int, int> nod;
typedef unsigned u;

vector <nod> LIST[N];
vector <int> d (N, 0);
vector <bool> M (N, 0);
queue <int> Q;
int n, m, S, D;

int main ()
{
    ifstream fin (in);
    fin >> n >> m >> S >> D;
    for (int i = 0; i < m; ++i) {
        int x, y, c;
        fin >> x >> y >> c;
        LIST[x].pb (nod (y,c));
        LIST[y].pb (nod (x, -c));
    }
    fin.close();
    Q.push (S);
    M[S] = 1;
    while (Q.size() && !M[D]) {
        int x = Q.front();
        Q.pop();
        for (u i = 0; i < LIST[x].size(); ++i)
            if (!M[LIST[x][i].y]) {
                M[LIST[x][i].y] = 1;
                d[LIST[x][i].y] = d[x] + LIST[x][i].c;
                Q.push (LIST[x][i].y);
            }
    }
    ofstream fout (out);
    fout << d[D];
    fout.close();
    return 0;
}