Cod sursa(job #3275534)

Utilizator _c_lucaCiobotaru Luca _c_luca Data 10 februarie 2025 20:42:53
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

int main(){
    ifstream in("sate.in");
    ofstream out("sate.out");
    int n, m, x0, y0;
    in >> n >> m >> x0 >> y0;

    vector <pair<int, int>> S[n+1];

    for(int i = 0; i < m; i++){
        int x, y, d;
        in>>x>>y>>d;
        S[x].push_back({y, d});
        S[y].push_back({x, d});
    }

    vector <int> d(n+1, 0);

    vector <int> vizitat(n+1, 0);

    queue<int> q;
    q.push(x0);

    int x;

    d[x]=0;
    vizitat[x]=1;

    while(!q.empty()){
        x = q.front();
        q.pop();

        for(auto y : S[x]){
            if(vizitat[y.first]==0){
                vizitat[y.first]=1;
                if(y.first > x){
                    d[y.first] = d[x] + y.second;
                }else{
                    d[y.first] = d[x] - y.second;
                }
                q.push(y.first);
            }
        }
    }

    out<<d[y0];
    
    in.close();
    out.close();
    return 0;
}