Cod sursa(job #2854281)

Utilizator LuciBBadea Lucian LuciB Data 21 februarie 2022 10:06:11
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");

const int NMAX = 3e4;

struct nod {
    int val, cost;
};

vector<nod> edges[NMAX + 1];
int d[NMAX + 1];
queue<nod> q;

void dfs(int node) {
    for(auto neighbour : edges[node]) {
        if(d[neighbour.val] == 0) {
            if(neighbour.val < node)
                d[neighbour.val] = d[node] - neighbour.cost;
            else
                d[neighbour.val] = d[node] + neighbour.cost;
            dfs(neighbour.val);
        }
    }
}

int main() {
    int n, m, x, y, a, b, c;
    fin >> n >> m >> x >> y;
    for(int i = 0; i < m; i++) {
        fin >> a >> b >> c;
        edges[a].push_back({b, c});
        edges[b].push_back({a, c});
    }
    d[x] = 1;
    dfs(x);
    fout << d[y] - 1;
    return 0;
}