Cod sursa(job #2373997)

Utilizator dan.cantorCantor Dan Alexandru dan.cantor Data 7 martie 2019 16:25:13
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>
#include <queue>
using namespace std;

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

void DFS(int x);
void ReadFunction();

const int maxn = 30001;
int n, m, x, y, d[maxn];
vector<vector<pair<int, int>>> G;
bool v[maxn];

int main()
{
    ReadFunction();
    DFS(x);
    fout << d[y];
}

void DFS(int x)
{
    int dist = 0;
    queue<int> q;
    q.push(x);
    v[x] = true;
    int nod;
    while (!q.empty())
    {
        nod = q.front();
        q.pop();

        for (const auto& p : G[nod])
        {
            if (!v[p.first])
            {
                if (nod < p.first)
                    d[p.first] = d[nod] + p.second;
                else
                    d[p.first] = d[nod] - p.second;
                q.push(p.first);
                v[p.first] = true;
            }
        }
    }
}

void ReadFunction()
{
    int a,b, d;
    fin >> n >> m >> x >> y;
    G = vector<vector<pair<int, int>>>(n + 1);
    for (int i = 1; i <= m; ++i)
    {
        fin >> a>> b >> d;
        G[a].push_back({b, d});
        G[b].push_back({a, d});
    }
}