Cod sursa(job #2550811)

Utilizator ArdeleanOficialAlexandru ArdeleanOficial Data 19 februarie 2020 10:06:43
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 30009;

bitset < N > viz;
vector < pair < int, int > > adia[N];

int dist[N];

void dfs(int nod) {
    for (auto i : adia[nod]) {
        if (!viz[i.first]) {
            dist[i.first] = dist[nod] + i.second;
            viz[i.first] = 1;
            dfs(i.first);
        }
    }
}

int main()
{
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    ios_base::sync_with_stdio(NULL);
    fin.tie(0);
    fout.tie(0);
    int n, m, x, y;
    fin >> n >> m >> x >> y;
    if (x > y)
        swap(x, y);
    int a, b, c;
    while (m--) {
        fin >> a >> b >> c;
        adia[a].push_back({b, c});
        adia[b].push_back({a, -c});
    }
    viz[x] = 1;
    dfs(x);
    if (!viz[y])
        return fout << -1, 0;
    fout << dist[y];
    return 0;
}