Pagini recente » Cod sursa (job #2719055) | Cod sursa (job #1976748) | Cod sursa (job #2352621) | Cod sursa (job #229369) | Cod sursa (job #3188269)
#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]) {
if (i.first <= y) {
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 ? i.second + q.front().val : q.front().val - i.second});
sateVazute[i.first] = true;
}
}
q.pop();
}
fout<<ans;
return 0;
}