Cod sursa(job #3188612)

Utilizator dragoncrackCandidatu Mario Luca dragoncrack Data 3 ianuarie 2024 15:26:25
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int n, m, x, y;
vector<pair<int, int>> neighbours[30005];
queue<int> Q;
int distances[30005];

int main()
{
    fin >> n >> m >> x >> y;
    for (int i = 1; i <= m; i++) {
        int a, b, length;
        fin >> a >> b >> length;
        neighbours[a].push_back(make_pair(b, length));
        neighbours[b].push_back(make_pair(a, -length));
    }
    Q.push(x);
    while (!Q.empty()) {
        int current = Q.front();
        for (int i = 0; i < neighbours[current].size(); i++) {
            int currentNeighbour = neighbours[current][i].first;
            if (distances[currentNeighbour] == 0) {
                distances[currentNeighbour] = distances[current] + neighbours[current][i].second;
                Q.push(currentNeighbour);
            }
        }
        Q.pop();
    }
    fout << distances[y];
}