Cod sursa(job #2029572)

Utilizator saba_alexSabadus Alex saba_alex Data 30 septembrie 2017 11:41:24
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int MAX = 30005;

int n, m, x, y, a, b, c;
int dist[MAX];
bool viz[MAX];

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

void bfs(){

    Q.push(x);
    viz[x] = 1;
    dist[x] = 0;
    while(!Q.empty()){
        int nod = Q.front();
        Q.pop();
        for(auto it: G[nod]){

            if(!viz[it.first]){

                viz[it.first] = 1;
                if(nod < it.first){
                    Q.push(it.first);
                    dist[it.first] = dist[nod] + it.second;
                }
                else{
                    Q.push(it.first);
                    dist[it.first] = dist[nod] - it.second;
                }
            }
        }
    }
}

int main()
{
    fin >> n >> m >> x >> y;

    for(int i = 1; i <= m; ++i){
        fin >> a >> b >> c;
        G[a].push_back(make_pair(b, c));
        G[b].push_back(make_pair(a, c));
    }

    bfs();

    fout << dist[y];

    return 0;
}