Pagini recente » Cod sursa (job #2221496) | Cod sursa (job #1934377) | Cod sursa (job #1902407) | Cod sursa (job #2791204) | Cod sursa (job #3165205)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
/// i, j , cost
vector <pair <int, int > > a[100030];
queue <int> q;
int n, m, x, y, cost[100030];
int bfs()
{
for (int i = 1; i <= n; ++i) {
cost[i] = -1;
}
q.push(x);
cost[x] = 0;
while(!q.empty()) {
int nod = q.front();
q.pop();
for (auto vecin : a[nod]) {
if (cost[vecin.first] == -1) {
cost[vecin.first] = cost[nod] + vecin.second;
q.push(vecin.first);
if (vecin.first == y) {
return cost[vecin.first];
}
}
}
}
}
int main()
{
f >> n >> m >> x >> y;
if (x > y) {
swap(x, y);
}
for (int i = 1; i <= m; ++i) {
int x1, y1, cost;
f >> x1 >> y1 >> cost;
a[x1].push_back({y1, cost});
a[y1].push_back({x1, -cost});
}
g << bfs();
return 0;
}