Cod sursa(job #3321014)

Utilizator tonealexandruTone Alexandru tonealexandru Data 7 noiembrie 2025 21:50:34
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

int save[100005], y;
vector<pair<int, int>> adj[100005];

int rez = -1;

void dfs(int nod, int cost)
{
    save[nod] = 1;

    if(nod != y && rez == -1)
    {
        for(auto x : adj[nod])
            if(save[x.first] == 0)
            {
                if(x.first < nod)
                    dfs(x.first, cost - x.second);
                else
                    dfs(x.first, cost + x.second);
            }
    }
    else if(rez == -1)
        rez = cost;
}

int main()
{
    ifstream cin("sate.in");
    ofstream cout("sate.out");
    int n, m, x, a, b, val, cnt = 0;
    cin>>n>>m>>x>>y;

    for(int i=0; i<m; i++)
    {
        cin>>a>>b>>val;
        adj[a].push_back({b, val});
        adj[b].push_back({a, val});
    }

    dfs(x, 0);

    cout<<rez;

    return 0;
}