Cod sursa(job #2966463)

Utilizator samyro14Samy Dragos samyro14 Data 17 ianuarie 2023 18:16:10
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("sate.in");
ofstream  fout("sate.out");
#define ll long long
#define pb push_back
#define fast_read  ios :: sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
const int maxn = 3e4;
const int maxm = 1e4;
#define INF 0x3f3f3f3f
int n, m, l, r;
vector<pair<int, int>> a[maxn + 2];
int d[maxn + 2];
void read(){
    fin >> n >> m >> l >> r;
    for(int i = 1; i <= m; ++i){
        int x, y, cost; fin >> x >> y >> cost;
        a[x].push_back({y, cost});
        a[y].push_back({x, cost});
    }
}
void bfs(){
    queue<int> q;
    q.push(l);
    while(!q.empty()){
        int i = q.front();
        q.pop();
        for(auto x : a[i]){
            if(!d[x.first]){
                if(i < x.first)
                    d[x.first] = d[i] + x.second;

                else d[x.first] = d[i] - x.second;
                q.push(x.first);
            }
        }
    }
    fout << d[r];
}
int main(){
    read();
    bfs();

    return 0;
}
/*

*/