Pagini recente » Cod sursa (job #2897270) | Cod sursa (job #3349978) | Cod sursa (job #3344638) | Cod sursa (job #3304340) | Cod sursa (job #3316244)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
typedef pair<int, int> pii;
int solve()
{
int n, m, a, b;
fin >> n >> m >> a >> b;
vector<vector<pii>> g(n + 1, vector<pii>());
int x, y, c;
for (int i = 0; i < m; ++i)
{
fin >> x >> y >> c;
g[x].push_back({y, c});
g[y].push_back({x, -c});
}
vector<int> dist(n + 1, -1);
dist[a] = 0;
queue<int> q;
q.push(a);
while (!q.empty())
{
int crt = q.front();
if (crt == b)
return dist[crt];
q.pop();
for (auto &p : g[crt])
{
if (dist[p.first] == -1)
{
q.push(p.first);
dist[p.first] = p.second + dist[crt];
}
}
}
return -1;
}
int main()
{
fout << solve() << '\n';
return 0;
}