Cod sursa(job #3140161)

Utilizator Radu_BicliBiclineru Radu Radu_Bicli Data 4 iulie 2023 15:05:09
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, i, j, x, y, p, q, d[30002];
vector<pair<int, int> > a[30002];
bitset<30002> fr;

static inline int Lee(int x, int y) {
    queue<int> q;
    d[x] = 0;
    fr[x] = 1;

    q.push(x);
    while(!q.empty()) {
        int curr = q.front();

        for(auto it : a[curr]) {
            int vec = it.first;
            int dist = it.second;
            if(!fr[vec]) {
                q.push(vec);
                fr[vec] = 1;
                d[vec] = d[curr] + dist;

                if(vec == y) return d[vec];
            }
        }
        q.pop();
    }
}

int main() {
    fin >> n >> m >> p >> q;
    while(fin >> x >> y >> m) {
        a[x].push_back({y, m});
        a[y].push_back({x, -m});
    }
    fout << Lee(p, q);

    return 0;
}