Cod sursa(job #3235596)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 19 iunie 2024 08:44:48
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>

std :: ifstream in ("sate.in");

std :: ofstream out ("sate.out");

const int NMAX = 3e4 + 5;

int n;

int m;

int x;

int y;

int a;

int b;

int d;

std :: vector <std :: pair<int, int>> v[NMAX];

int dist[NMAX];

std :: queue <int> q;

void bfs()
{
    while(!q.empty())
    {

        int nod = q.front();

        q.pop();

        if(nod == y)
        {
            out << dist[nod] - 1;
            return ;
        }

        for(auto i : v[nod])
        {
            if(dist[i.first] == 0)
            {
                if(i.first < nod)// ma intorc deci scad
                {
                    q.push(i.first);
                    dist[i.first] = dist[nod] - i.second;
                }
                else// merg in fata deci cresc
                {
                    q.push(i.first);
                    dist[i.first] = dist[nod] + i.second;
                }
            }
        }
    }
}

int main()
{

    in >> n >> m >> x >> y;

    for(int i = 1; i <= m; i ++)
    {
        in >> a >> b >> d;

        v[a].push_back(std :: make_pair(b, d));

        v[b].push_back(std :: make_pair(a, d));
    }

    dist[x] = 1;

    q.push(x);

    bfs();

    return 0;
}