Cod sursa(job #3225072)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 16 aprilie 2024 21:16:26
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");
const int inf = 1e7 + 2;
struct Muchie {
    int x, y, c;
};
vector<Muchie> a[30002];
int n, m, p, q, i, x, y, c;
bool fr[30002];

static inline int Bfs(int nod) {
    queue<pair<int, int>> coada;
    coada.push({nod, 0});

    while(!coada.empty()) {
        auto nod = coada.front();
        coada.pop();

        fr[nod.first] = true;

        if(nod.first == q) return nod.second;

        for(auto vec : a[nod.first]) {
            if(!fr[vec.y]) {
                fr[vec.y] = true;
                coada.push({vec.y, nod.second + vec.c});
            }
        }
    }
}

int main() {
    fin >> n >> m >> p >> q;
    for(i = 1; i <= m; i++) {
        fin >> x >> y >> c;
        a[x].push_back({x, y, c});
        a[y].push_back({y, x, -c});
    }

    fout << Bfs(p);

    return 0;
}