Pagini recente » Cod sursa (job #2479228) | Cod sursa (job #1852954) | Cod sursa (job #1326361) | Cod sursa (job #2820042) | Cod sursa (job #2027642)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 30000;
int n, m, x, y, a, b, c, ans;
vector<pair<int, int>> g[NMAX + 5];
queue<pair<int, int>> q;
int Solve() {
q.push(make_pair(x, 0));
while (!q.empty()) {
int node = q.front().first;
int cost = q.front().second;
q.pop();
vector<pair<int, int>>::iterator it;
for(it = g[node].begin(); it != g[node].end(); ++it) {
pair<int, int> sat = *it;
if(node > sat.first)
sat.second = cost - sat.second;
else
sat.second = cost + sat.second;
if(sat.first == y) {
return sat.second;
} else {
q.push(sat);
}
}
}
return 0;
}
int main() {
ifstream cin("sate.in");
ofstream cout("sate.out");
cin >> n >> m >> x >> y;
for(int i = 1; i <= m; ++i) {
cin >> a >> b >> c;
g[a].push_back(make_pair(b, c));
g[b].push_back(make_pair(a, c));
}
cout << Solve() << "\n";
return 0;
}