Cod sursa(job #2524884)

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

using namespace std;

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

const int NMAX = 100005;

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

int N, M, X, Y;
int x, y, c;
int D[NMAX];

void Citire()
{

}

void BFS()
{

}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);

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

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

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

    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 0;
                }
            }
            Q.push(it.first);
            visited[it.first] = 1;
        }
    }
}