Cod sursa(job #2524333)

Utilizator MichaelXcXCiuciulete Mihai MichaelXcX Data 15 ianuarie 2020 14:32:00
Problema Sate Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

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

const short NMAX = 30005;

vector< pair< int, int > > G[NMAX];
queue< int > Q;

int N, M, X, Y;
int D[NMAX];
char visited[NMAX];

void Citire()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);

    fin >> N >> M >> X >> Y;

    for(int i = 0; i < M; i++) {
        int x, y, c;
        fin >> x >> y >> c;
        G[x].push_back(make_pair(y, c));
        G[y].push_back(make_pair(x, c));
    }

    D[X] = 0;
    visited[X] = 1;
    Q.push(X);
}

void BFS()
{
    while(!Q.empty()) {
        int currentNode = Q.front();
        Q.pop();

        for(auto it : G[currentNode]) {
            if(!visited[it.first]) {
                if(it.first > currentNode)
                    D[it.first] = D[currentNode] + it.second;
                else
                    D[it.first] = D[currentNode] - it.second;

                if(it.first == Y) {
                    fout << D[Y] << "\n";
                    return;
                }
            }
            Q.push(it.first);
            visited[it.first] = 1;
        }
    }
}

int main()
{
    Citire();
    BFS();
}