Cod sursa(job #1202374)

Utilizator diana97Diana Ghinea diana97 Data 27 iunie 2014 20:36:50
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int NMAX = 30000 + 1;

int n, x, y;
vector < pair <int, int> > v[NMAX];
queue <int> q;
char viz[NMAX];
int d[NMAX];

void citeste () {
    int m, a, b, c;
    f >> n >> m >> x >> y;
    for (int i = 1; i <= m; i++) f >> a >> b >> c, v[a].push_back(make_pair(b, c)), v[b].push_back(make_pair(a, -c));
}

/*void rezolva () {
    q.push(x);
    viz[x] = 1;
    int a, b, l;
    while (!q.empty()) {
        a = q.front();
        q.pop();
        if (a != y) {
            l = v[a].size();
            for (int i = 0; i < l; i++) {
                b = v[a][i].first;
                if (viz[b] == 0) {
                    viz[b] = 1;
                    d[b] = d[a] + v[a][i].second;
                    q.push(b);
                }
            }
        }
    }
    g << d[y] << '\n';

}*/

void DF (int a) {
    if (a != y) {
        int b, l;
        l = v[a].size();
        for (int i = 0; i < l; i++) {
            b = v[a][i].first;
            if (viz[b] == 0) {
                viz[b] = 1;
                d[b] = d[a] + v[a][i].second;
                DF(b);
            }
        }
    }
}

int main () {
    citeste ();
    //rezolva ();
    DF(x);
    g << d[y] << ' ';
    return 0;
}