Cod sursa(job #3312631)

Utilizator prodsevenStefan Albu prodseven Data 29 septembrie 2025 11:49:02
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

ifstream cin("sate.in");
ofstream cout("sate.out");

vector<vector<pair<int, int>>> sate;
int n, m, x, y;
queue<int> q;
vector<int> distanta(30003, -1);

void bfs() {
    q.push(x);
    distanta[x] = 0;
    while (!q.empty()) {
        int current = q.front();
        q.pop();
        for (pair<int, int> neighbor : sate[current]) {
            if (distanta[neighbor.first] == -1) {
                distanta[neighbor.first] = distanta[current] + neighbor.second;
                q.push(neighbor.first);
            }
        }
    }
}

int main() {
    cin >> n >> m >> x >> y;
    sate.resize(n + 2);
    for (int i = 1 ; i <= m ; ++i) {
        int src, dest, dist;
        cin >> src >> dest >> dist;
        sate[src].push_back({dest, dist});
        sate[dest].push_back({src, -dist});
    }
    bfs();
    cout << distanta[y];
    return 0;
}