Cod sursa(job #2705413)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 12 februarie 2021 16:14:37
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
#define debug(v,n) for (int i = 1; i <= (n); ++i) cout << v[i] << " ";
#define next cout << '\n'
using namespace std;

const int N = 30005;
vector<pair<int,int>> graf[N];
int n, m, x, y;
queue<pair<int,int>> coada;

int main() {
    //ifstream fin("date.in.txt");
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    fin >> n >> m >> x >> y;

    if(x > y)
        swap(x,y);

    for (int i = 1; i <= m; ++i) {
        int a, b, d;
        fin >> a >> b >> d;
        graf[a].push_back({b, d});
        graf[b].push_back({a, d});
    }
    for (pair<int,int> i : graf[x]) {
        int to = i.first, d = i.second;
        if(to < x)
            coada.push({to, -d});
        else
            coada.push({to, d});
    }
    while(!coada.empty()) {
        int nod = coada.front().first;
        int act = coada.front().second;
        coada.pop();

        if(nod == y) {
            fout << act;
            return 0;
        }

        for (pair<int,int> i : graf[nod]) {
            int to = i.first, d = i.second;
            if(to < act)
                coada.push({to, act - d});
            else
                coada.push({to, act + d});
        }
    }
    return 0;
}