Cod sursa(job #3374890)

Utilizator MironTudorTudor Miron MironTudor Data 9 noiembrie 2026 18:00:00
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <bits/stdc++.h>
#define INF 2e9

using namespace std;

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

int n, m, X, Y;
vector <vector<pair<int,int>>> adj;
vector <int> dist;


void DFS(int nod) {
    for (auto i : adj[nod]) 
        if (dist[i.first] == 0) {
            dist[i.first] = dist[nod] + i.second;
            DFS(i.first);
        }
}

int main () {
    fin >> n >> m >> X >> Y;
    adj.resize(n + 5);
    dist.reserve(n + 5);
    for (int i = 1; i <= m;  i++) {
        int x, y, cost;
        fin >> x >> y >> cost;
        if (x > y)
            swap(x, y);
        adj[x].push_back({y, cost});
        adj[y].push_back({x, -cost});
    }    
    DFS(X);
    fout << dist[Y] << "\n";
    return 0;
}