Pagini recente » Cod sursa (job #3210823) | Cod sursa (job #1429118) | Cod sursa (job #1116073) | Cod sursa (job #45904) | Cod sursa (job #3188270)
#include <bits/stdc++.h>
#define MAX_N 30001
#define fin cin
#define fout cout
using namespace std;
//ifstream fin("sate.in");
//ofstream fout("sate.out");
struct cv {
int sat;
int val;
};
int n, m, x, y;
int ans;
queue<cv> q;
bool sateVazute[MAX_N];
int main() {
fin >> n >> m >> x >> y;
sateVazute[x] = true;
vector<map<int, int>> drumuri(n + 1);
for (int i = 1; i <= m; i++) {
int temp_x, temp_y, temp_d;
fin >> temp_x >> temp_y >> temp_d;
drumuri[temp_x][temp_y] = temp_d;
drumuri[temp_y][temp_x] = -temp_d;
}
for (const auto &i: drumuri[x]) {
q.push({i.first, i.second});
sateVazute[i.first] = true;
}
while (!q.empty()) {
if (q.front().sat == y) {
ans = q.front().val;
break;
}
for (const auto & i : drumuri[q.front().sat]) {
if(!sateVazute[i.first]) {
q.push({i.first,i.second + q.front().val});
sateVazute[i.first] = true;
}
}
q.pop();
}
fout<<ans;
return 0;
}