Cod sursa(job #3372969)

Utilizator Dani111Gheorghe Daniel Dani111 Data 5 noiembrie 2026 18:29:16
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <bits/stdc++.h>
using namespace std;

int main() {
    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int N, M, X, Y; cin >> N >> M >> X >> Y;

    vector<vector<pair<int, int>>>G(N + 3);
    for(int i = 1; i <= M; i++) {
        int x, y, c; cin >> x >> y >> c;
        G[x].push_back({y, c});
        G[y].push_back({x, c});
    }

    vector<int>cost(N + 3, -1);
    queue<int>q;
    cost[X] = 0;
    q.push(X);
    while(!q.empty()) {
        for(auto i : G[q.front()]) {
            if(cost[i.first] == -1) {
                if(q.front() < i.first)
                cost[i.first] = cost[q.front()] + i.second;
                else cost[i.first] = cost[q.front()] - i.second;
                q.push(i.first);
            }
        }
        q.pop();
    }   

    // for(int i = 1; i <= N; i++) cout << cost[i] << ' ';


    cout << cost[Y];

}