Cod sursa(job #2825587)

Utilizator rares89_Dumitriu Rares rares89_ Data 4 ianuarie 2022 21:15:19
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>

using namespace std;

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

int n, m, x, y, s;
vector< pair<int, int> > G[30005];
bitset<30005> v;

void bfs(int nd) {
    queue<int> Q;
    Q.push({nd});
    v[nd] = true;
    while(!Q.empty()) {
        int nod = Q.front();
        Q.pop();
        for(auto it : G[nod]) {
            if(!v[it.first]) {
                v[it.first] = true;
                s += it.second;
                if(it.first == y) {
                    fout << s;
                    exit(0);
                }
                Q.push(it.first);
            }
        }
    }
}

int main() {
    fin >> n >> m >> x >> y;
    for(int i = 1; i <= m; i++) {
        int a, b, c;
        fin >> a >> b >> c;
        G[a].push_back({b, c});
        G[b].push_back({a, -c});
    }
    bfs(x);
    return 0;
}