Cod sursa(job #2241465)

Utilizator EclipseTepes Alexandru Eclipse Data 15 septembrie 2018 23:58:15
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#define dMAX 31000

using namespace std;

typedef pair<int, int> pi;

int n, m, x, y;
int a, b, c;
int dist[dMAX];
bool viz[dMAX];
vector<pi> graf[dMAX];
deque<int> d;
int pVerif, newV;

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

int main()
{
    int i, j;
    fin >> n >> m >> x >> y;
    for (i = 1; i <= m; i++) {
        fin >> a >> b >> c;
        graf[a].push_back({b, c});
        graf[b].push_back({a, c});
    }
    if (y < x) swap(x, y);
    d.push_back(x);
    dist[x] = 1;
    viz[x] = true;
    while (!d.empty()) {
        pVerif = d.front();
        viz[pVerif] = true;
        d.pop_front();
        for (i = 0; i < graf[pVerif].size(); i++) {
            newV = graf[pVerif][i].first;
            if (!viz[newV]) {
                if (newV > pVerif) {
                    dist[newV] = dist[pVerif] + graf[pVerif][i].second;
                    //cout << dist[newV] << ' ';
                } else {
                    dist[newV] = dist[pVerif] - graf[pVerif][i].second;
                    //cout << dist[newV] << ' ';
                }
                //cout << newV << ' ';
                d.push_back(newV);
            }
        }
    }
    fout << dist[y] - 1;
    return 0;
}