Cod sursa(job #3372768)

Utilizator licadariaLica Daria licadaria Data 4 noiembrie 2026 22:03:45
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include<utility>
#include<vector>
#include<fstream>
using namespace std;
int N, M, x,y, i,j,D;
bool gasit = false;
vector<vector<pair<int, int>>> matrice;
vector<int> vizitat;
ofstream fout("sate.out");

int Dfs(int curent, int distanta){
    if (vizitat[curent] != 0){ return 0;;}
    else { vizitat[curent] = 1;}
    int dis_vecin;
    for (int t = 0; t<matrice[curent].size(); t++) {
        if (gasit){return 0;}
        int vecin = matrice[curent][t].first;
        if (vecin>curent){dis_vecin = distanta+matrice[curent][t].second;}
        else{dis_vecin = distanta-matrice[curent][t].second;}
        if (vecin == y) {
            gasit = true;
            fout<<dis_vecin;
            return 0;}
        else Dfs(vecin, dis_vecin);

    }
    return 0;}


int main() {
    ifstream fin("sate.in");
    fin>>N>>M>>x>>y;
    matrice.resize(N+1);

    for (int t =0; t<M; t++) {
        fin>>i>>j>>D;
        matrice[i].push_back({j, D});
        matrice[j].push_back({i, D});
    }
    fin.close();
    vizitat.resize(N+1);
    for (int t = 0; t<=N; t++){vizitat[t] = 0;}
    vizitat[x] = 1;

    for (int t=0; t<matrice[x].size(); t++) {
        if (gasit) break;
        else {
            if (matrice[x][t].first>x) Dfs(matrice[x][t].first, matrice[x][t].second);
            else Dfs(matrice[x][t].first, - matrice[x][t].second);
        }
    }
    return 0;

}