Cod sursa(job #3004323)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 16 martie 2023 11:35:54
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream cin("sate.in");
ofstream cout("sate.out");

const int MAX = 3e4 + 1;

int dp[MAX] , n , x , y , m , c , a , b;

bool viz[MAX];

vector <pair<int,int>> g[MAX];

queue <int> q;

int main(){

    cin >> n >> m >> x >> y;

    while(m--){

        cin >> a >> b >> c;

        g[a].push_back({b,c});
        g[b].push_back({a,c});
    }

    dp[x] = 0;

    q.push(x);

    while(!q.empty()){

        x = q.front();

        viz[x] = 1;

        q.pop();

        for(auto it : g[x]){

            if(!viz[it.first] && it.first > x){

                dp[it.first] = dp[x] + it.second;

                q.push(it.first);

            }else if(!viz[it.first] && it.first < x){

                dp[it.first] = dp[x] - it.second;

                q.push(it.first);
            }
        }
    }

    cout << dp[y];

    return 0;
}