Cod sursa(job #3372920)

Utilizator irina_opreaIrina Oprea irina_oprea Data 5 noiembrie 2026 15:33:11
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const int NMAX = 3e4+10;
const int INF = 2e9;

vector <vector <pair <int, pair<int, int>>>> nodes(NMAX);

long long dist[NMAX];
bool processed[NMAX];

void dfs (int a);

int main()
{
    ifstream cin ("sate.in");
    ofstream cout ("sate.out");

    int n, m, st, nd, x, y, lgth;
    cin >> n >> m >> st >> nd;
    if (st > nd) swap(st, nd);
    for (int i=0; i<m; i++)
    {
        cin >> x >> y >> lgth;
        if (x > y) swap(x, y);
        nodes[x].push_back({y, {lgth, 1}});
        nodes[y].push_back({x, {lgth, -1}});
    }
    dfs(st);
    cout << dist[nd] << '\n';
    return 0;
}
void dfs (int a)
{
    if (processed[a]) return;
    processed[a] = true;
    for (auto it : nodes[a])
    {
        dist[it.first] = dist[a] + (it.second.first * it.second.second);
        dfs(it.first);
    }
}