Pagini recente » Cod sursa (job #2386834) | Cod sursa (job #1578311) | Cod sursa (job #2787335) | Cod sursa (job #2543833) | Cod sursa (job #1202377)
#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) {
// cout << nod << ' ' << cost << endl;
this->nod = nod;
this->cost = cost;
}
};
vector <muchie> v[30001];
int d[30001];
char viz[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) {
viz[nod] = 1;
int l = v[nod].size();
for (int i = 0; i < l; i++)
if (viz[v[nod][i].nod] == 0) {
d[v[nod][i].nod] = d[nod] + v[nod][i].cost;
DFS(v[nod][i].nod);
}
}
}
int main () {
citeste ();
DFS (x);
g << d[y] << '\n';
return 0;
}