Cod sursa(job #2513352)

Utilizator DariusDCDarius Capolna DariusDC Data 22 decembrie 2019 22:12:48
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
#define nmax 30001

using namespace std;

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

int d[nmax];
int n, m;
int x, y;
bool viz[nmax];
queue <int> q;

vector < pair < int,int  > > muchii[nmax];

inline void bfs()
{
    viz[x] = true;
    q.push(x);
    while (!q.empty())
    {
        int nod = q.front();
        q.pop();
        for (auto v : muchii[nod])
        {
            if (!viz[v.first])
            {
                viz[v.first] = true;
                q.push(v.first);
                if (v.first > nod)
                    d[v.first] = d[nod] + v.second;
                else
                    d[v.first] = d[nod] - v.second;
                if (v.first == y)
                    return;
            }
        }
    }
}

int main()
{
    fin >> n >> m >> x >> y;
    for (int i = 1; i<= m; i++)
    {
        int a, b, c;
        fin >> a >> b >> c;
        muchii[a].push_back(make_pair(b,c));
        muchii[b].push_back(make_pair(a,c));
    }
    bfs();
    fout << d[y];
    return 0;
}