Pagini recente » Cod sursa (job #2813500) | Cod sursa (job #1376701) | Cod sursa (job #419041) | Cod sursa (job #2639333) | Cod sursa (job #2560641)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define pi pair<int,int>
using namespace std;
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();
if (z == y)
return ans[y];
for (auto t : l[z])
if (!viz[t.first]) {
viz[t.first] = 1;
ans[t.first] = ans[z] + t.second;
q.push(t.first);
}
}
}
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;
cin >> n >> m >> x >> y;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> d;
l[u].push_back({ v,d });
l[v].push_back({ u,-d });
}
cout << solve(x,y);
}