Cod sursa(job #2767181)

Utilizator MihneaCadar101Cadar Mihnea MihneaCadar101 Data 5 august 2021 09:54:24
Problema Sate Scor 65
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");

const int nmax = 3e4 + 5;
int n, m, x, y, ans;
vector <pair <int, int> > v[nmax];
bool rezolvat, vizitat[nmax];

void dfs(int x) {
    vizitat[x] = 1;
    for (pair <int, int> i : v[x]) {
        if (vizitat[i.first]) continue;

        if (i.first < x) ans -= i.second;
        else ans += i.second;

        if (i.first == y) {
            rezolvat = true;
            return;
        }

        dfs(i.first);
        if (rezolvat) return;

        if (i.first < x) ans += i.second;
        else ans -= i.second;

    }

    vizitat[x] = 0;
    return;
}

int main()
{
    fin >> n >> m >> x >> y;
    for (int k = 1; k <= m; ++k) {
        int i, j, dist;
        fin >> i >> j >> dist;
        v[i].push_back({j, dist});
        v[j].push_back({i, dist});
    }

    dfs(x);
    fout << ans;
    return 0;
}