Cod sursa(job #3374887)

Utilizator teogrecigrecioiu teo teogreci Data 9 noiembrie 2026 17:51:32
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <algorithm>
#include <fstream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

const int nmax = 100000;

vector<pair<int,int>> adj[nmax];
int dist[nmax];
bool viz[nmax];

int main() {
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    int n, m, x, y;
    fin >> n >> m >> x >> y;
    for (int k = 0; k < m; k++) {
        int i, j, d;
        fin >> i >> j >> d;
        adj[i].push_back({j, d});
        adj[j].push_back({i, -d});
    }
    if (x > y) {
        swap(x, y);
    }
    queue<int> q;
    q.push(x);
    viz[x] = true;
    dist[x] = 0;

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (u == y) {
            break;
        }
        for (int i = 0; i < adj[u].size(); i++) {
            int v = adj[u][i].first;
            int cost = adj[u][i].second;

            if (!viz[v]) {
                viz[v] = true;
                dist[v] = dist[u] + cost;
                q.push(v);
            }
        }
    }
    fout << dist[y] << '\n';
    return 0;
}