Cod sursa(job #2524328)

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

using namespace std;

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

const int NMAX = 30005;

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

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

void Citire()
{
    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));
    }

    memset(D, -1, sizeof(D));
    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();
}