Cod sursa(job #2933514)

Utilizator mihaicrisanMihai Crisan mihaicrisan Data 5 noiembrie 2022 11:20:44
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <bits/stdc++.h>
#include <vector>
#include <queue>

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];
int d[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 bfs() {
    d[x] = 1;
    queue < pair <int, int> > q;
    q.push({1, x});
    while (!q.empty()) {
        int nod = q.front().second;
        q.pop();
        for (int i = 0; i < g[nod].size(); i++) {
            int v = g[nod][i].vf;
            int c = g[nod][i].c;
            if (!d[v]) {
                d[v] = d[nod] + c;
                q.push({d[v], v});
            }
        }
    }
}

int main() {
    read();
    bfs();
    fout << d[y] - 1;
    return 0;
}