Pagini recente » Cod sursa (job #1927598) | Cod sursa (job #1003895) | Cod sursa (job #2120380) | Cod sursa (job #3160500) | Cod sursa (job #2560659)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define pi pair<int,int>
using namespace std;
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
const int nmax = 3e4 + 5;
vector<pi>l[nmax];
queue<int>q;
bool viz[nmax];
int ans[nmax];
int solve(int x, int y) {
q.push(x);
viz[x] = 1;
while (!q.empty()) {
int z = q.front();
q.pop();
for (auto t : l[z])
if (!viz[t.first]) {
viz[t.first] = 1;
ans[t.first] = ans[z] + t.second;
q.push(t.first);
if (t.first == y)
return ans[y];
}
}
}
int main()
{
ifstream cin("sate.in");
ofstream cout("sate.out");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, x, y, u, v, d;
scan(n);
scan(m);
scan(x);
scan(y);
for (int i = 1; i <= m; i++) {
scan(u);
scan(v);
scan(d);
l[u].push_back({ v,d });
l[v].push_back({ u,-d });
}
if (x == y) {
cout << 0;
return 0;
}
cout << solve(x,y);
}