Cod sursa(job #3165205)

Utilizator stefanrotaruRotaru Stefan-Florin stefanrotaru Data 5 noiembrie 2023 17:24:39
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream f("sate.in");
ofstream g("sate.out");

/// i, j , cost
vector <pair <int, int > > a[100030];

queue <int> q;

int n, m, x, y, cost[100030];

int bfs()
{
    for (int i = 1; i <= n; ++i) {
        cost[i] = -1;
    }

    q.push(x);
    cost[x] = 0;

    while(!q.empty()) {
        int nod = q.front();
        q.pop();

        for (auto vecin : a[nod]) {
            if (cost[vecin.first] == -1) {
                cost[vecin.first] = cost[nod] + vecin.second;
                q.push(vecin.first);

                if (vecin.first == y) {
                    return cost[vecin.first];
                }
            }
        }
    }
}

int main()
{
    f >> n >> m >> x >> y;

    if (x > y) {
        swap(x, y);
    }

    for (int i = 1; i <= m; ++i) {
        int x1, y1, cost;
        f >> x1 >> y1 >> cost;
        a[x1].push_back({y1, cost});
        a[y1].push_back({x1, -cost});
    }

    g << bfs();

    return 0;
}