Cod sursa(job #2024073)

Utilizator StefanManolacheManolache Stefan StefanManolache Data 19 septembrie 2017 21:40:20
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <cstdio>
#include <vector>

FILE *fin = fopen("sate.in", "r");
FILE *fout = fopen("sate.out", "w");

#define maxn 120000
#define ll long long

std::vector<std::pair<int, int>> v[maxn + 1];
int viz[maxn + 1];
ll rez = 0;
int a, b;
void dfs(int t) {
    viz[t] = 1;
    int p, c;
    for (std::vector<std::pair<int, int>>::iterator it = v[t].begin(); it != v[t].end(); it++) {
        p = it->first;
        c = it->second;
        if (viz[p] != 1) {
            rez += c;
            if (p != b)
                dfs(p);
        }
    }
}

int main()
{

    int n, m, x, y, c;

    fscanf(fin, "%d%d%d%d", &n, &m, &a, &b);
    for (int i = 1; i <= m; i++) {
        fscanf(fin, "%d%d%d", &x, &y, &c);
        v[x].push_back (std::make_pair(y, c));
        v[y].push_back (std::make_pair(x, -c));
    }
    dfs(a);
    fprintf(fout, "%lld", rez);
    return 0;
}