Cod sursa(job #2933448)

Utilizator mihaicrisanMihai Crisan mihaicrisan Data 5 noiembrie 2022 10:53:28
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
#include <vector>

using namespace std;

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

const int INF = 1e9;
const int NMAX = 30005;

struct node {
    int vf;
    int c;
};

int n, m, x, y;
vector<node> g[NMAX];
vector <int> d(NMAX, INF), v(NMAX);

void read() {
    fin >> n >> m >> x >> y;
    int a, b, c;

    while (m--) {
        fin >> a >> b >> c;
        g[a].push_back({b, c});
        g[b].push_back({a, -c});
    }
}

void dfs(int nod) {
    if (nod == y)
        return;
    v[nod] = 1;
    for (int i = 0; i < g[nod].size(); i++) {
        int nv = g[nod][i].vf;
        int cost = g[nod][i].c;
        if (!v[nv]) {
            d[nv] = d[nod] + cost;
            dfs(nv);
        }
    }
}

int main() {
    d[x] = 0;
    dfs(x);
    fout << d[y];
    return 0;
}