Pagini recente » Cod sursa (job #3157836) | Cod sursa (job #1380684) | Cod sursa (job #2417632) | Cod sursa (job #2954) | Cod sursa (job #2702319)
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
#define debug(v,n) for (int i = 1; i <= (n); ++i) cout << v[i] << " ";
#define next cout << '\n'
using namespace std;
const int N = 30005;
vector<pair<int,int>> graf[N];
int n, m, x, y;
bool in[N];
struct cmp {
bool operator()(int a, int b) {
return sz(graf[a]) > sz(graf[b]);
}
};
priority_queue<int, vector<int>, cmp> coada;
int main() {
//ifstream fin("date.in.txt");
ifstream fin("sate.in");
ofstream fout("sate.out");
fin >> n >> m >> x >> y;
for (int i = 1; i <= m; ++i) {
int a, b, d;
fin >> a >> b >> d;
graf[a].push_back({b, d});
graf[b].push_back({a, d});
}
for (int i = x; i <= y; ++i) {
coada.push(i);
in[i] = true;
}
while(!coada.empty()) {
int nod = coada.top();
coada.pop();
in[nod] = false;
for (int i = 0; i < sz(graf[nod]); ++i) {
int act = graf[nod][i].first, lgAct = graf[nod][i].second;
if(act > y || act < x)
continue;
for (int j = i + 1; j < sz(graf[nod]); ++j) {
int to = graf[nod][j].first, lg = graf[nod][j].second;
if(to > y || to < x)
continue;
if(to > act) {
graf[act].push_back({to, lg + lgAct});
graf[to].push_back({act, lg + lgAct});
if(in[to] == false) {
coada.push(to);
in[to] = true;
}
}
else {
graf[to].push_back({act, lg - lgAct});
graf[act].push_back({to, lg - lgAct});
if(in[to] == false) {
coada.push(to);
in[to] = true;
}
}
}
}
}
for (int i = 0; i < sz(graf[x]); ++i) {
if(graf[x][i].first == y) {
fout << graf[x][i].second;
return 0;
}
}
return 0;
}