Cod sursa(job #2513355)

Utilizator DariusDCDarius Capolna DariusDC Data 22 decembrie 2019 22:17:43
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 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;
int viz[nmax];
queue <int> q;

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

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({b,c});
        muchii[b].push_back({a,c});
    }
    viz[x] = 1;
    q.push(x);
    while (!q.empty())
    {
        int nod = q.front();
        q.pop();
        for (auto v : muchii[nod])
        {
            if (!viz[v.first])
            {
                viz[v.first] = 1;
                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)
                {
                    fout << d[y];
                    return 0;
                }
            }
        }
    }
    fout << d[y];
    return 0;
}