Cod sursa(job #2829142)

Utilizator mihaistamatescuMihai Stamatescu mihaistamatescu Data 8 ianuarie 2022 12:40:56
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;
struct drum {
    int vec, dis;
};
vector<drum> L[30010];
queue<int> q;
bool f[30010];
int dm[30010], n, m, X, Y, a, b, d;

int main() {
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    fin >> n >> m >> X >> Y;
    if (X > Y) {
        swap(X, Y);
    }
    for (int i = 1; i <= m; i++) {
        fin >> a >> b >> d;
        L[a].push_back({b, d});
        L[b].push_back({a, -d});
    }
    q.push(X);
    dm[X] = 0;
    while (!q.empty()) {
        int crt = q.back();
        for (auto &x : L[crt]) {
            if (!f[x.vec]) {
                dm[x.vec] = dm[crt] + x.dis;
                q.push(x.vec);
                f[x.vec] = true;
            }
        }
        q.pop();
    }
    fout << dm[Y];
    return 0;
}