Cod sursa(job #2941875)

Utilizator LucaMuresanMuresan Luca Valentin LucaMuresan Data 18 noiembrie 2022 15:03:09
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

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

int d[30001];
bitset<30001>vis;

vector<pair<int, int>>v[30001];

int main()
{
    int n, m, x, y;
    in >> n >> m >> x >> y;

    while (m--)
    {
        int p, q, c;
        in >> p >> q >> c;
        v[p].push_back({q, c});
        v[q].push_back({p, c});
    }

    d[x] = 0;
    vis[x] = true;

    queue<int>q;
    q.push(x);

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

        for (auto p : v[x])
        {
            int i = p.first;
            if (!vis[i])
            {
                q.push(i);
                vis[i] = true;
                d[i] = d[x] - p.second * (i < x? 1 : -1);
            }
        }
    }

    out << d[y];

    return 0;
}