Cod sursa(job #2496792)

Utilizator dnprxDan Pracsiu dnprx Data 21 noiembrie 2019 17:39:09
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>
using namespace std;
 ///         sat  d
vector<pair<int, int> > a[30001];
queue<int> q;
int d[30003];
bitset<30003> viz;
ifstream fin("sate.in");
ofstream fout("sate.out");

int main()
{
    int i, sat, X, Y, n, m, x, y, dist;
    fin >> n >> m >> X >> Y;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y >> dist;
        if (x > y) swap(x, y);
        a[x].push_back({y, dist});
        a[y].push_back({x, -dist});
    }
    q.push(X);
    while (!q.empty())
    {
        x = q.front();
        q.pop();
        for (auto w : a[x])
        {
            sat = w.first;
            dist = w.second;
            if (viz[sat] == 0)
            {
                viz[sat] = 1;
                d[sat] = d[x] + dist;
                q.push(sat);
                if (sat == Y)
                {
                    fout << d[Y] << "\n";
                    return 0;
                }
            }
        }
    }
    return 0;
}