Cod sursa(job #2829403)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 8 ianuarie 2022 16:22:22
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <bits/stdc++.h>
#define DIM 30005

using namespace std;

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

int n, m, s, d, drum[DIM];
vector <pair <int, int>> edges[DIM];
bitset <DIM> v;
queue <int> q;

int main()
{
    f >> n >> m >> s >> d;
    if (s > d)
        swap(s, d);

    for (int i = 1; i <= m; i++)
    {
        int x, y, cost;
        f >> x >> y >> cost;
        if (x > y)
            cost = -cost;

        edges[x].push_back(make_pair(y, cost));
        edges[y].push_back(make_pair(x, -cost));
    }

    v[s] = 1;
    q.push(s);
    while (!q.empty())
    {
        int nod = q.front();
        q.pop();

        for (auto child : edges[nod])
        {
            if (!v[child.first])
            {
                v[child.first] = 1;
                drum[child.first] = drum[nod] + child.second;
                if (child.first == d)
                {
                    g << drum[child.first];
                    return 0;
                }
                q.push(child.first);
            }
        }
    }

    return 0;
}