Pagini recente » Cod sursa (job #2074078) | Cod sursa (job #1295114) | Cod sursa (job #759764) | Cod sursa (job #1795730) | Cod sursa (job #1202364)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f ("sate.in");
ofstream g ("sate.out");
int n, x, y;
struct muchie {
int nod, cost;
muchie(int nod, int cost) {
this->nod = nod;
this->cost = cost;
}
};
vector <muchie> v[30001];
int d[30001];
void citeste () {
int m, a, b, c;
f >> n >> m >> x >> y;
for (int i = 1; i <= m; i++) f >> a >> b >> c, v[a].push_back(muchie(b, c)), v[b].push_back(muchie(a, c));
}
void DFS (int nod) {
if (nod != y) {
int l = v[nod].size();
for (int i = 0; i < l; i++)
if (d[v[nod][i].nod] == -1) {
if (v[nod][i].nod < nod) d[v[nod][i].nod] = d[nod] - v[nod][i].cost;
else d[v[nod][i].nod] = d[nod] + v[nod][i].cost;
DFS(v[nod][i].nod);
}
}
}
int main () {
citeste ();
for (int i = 2; i <= n; i++) d[i] = -1;
DFS (x);
g << d[y] << '\n';
return 0;
}