#include <bits/stdc++.h>
using
namespace
std;
#define N 100005
vector<pair<
int
,
int
> > incr[N], decr[N];
int
_incr[N], _decr[N], shortest[N];
int
n, m, src, dest, MAXI = 1LL << 30;
void
Add_edge(
int
x,
int
y,
int
w)
{
incr[x].push_back({ w, y });
incr[y].push_back({ w, x });
decr[x].push_back({ -w, y });
decr[y].push_back({ -w, x });
}
int
Modified_Dijkstra()
{
priority_queue<pair<pair<
int
,
int
>, pair<
int
,
int
> > > q;
for
(
int
i = 1; i <= n; i++) {
sort(incr[i].begin(), incr[i].end());
sort(decr[i].begin(), decr[i].end());
}
for
(
int
i = 1; i <= n; i++)
shortest[i] = MAXI;
q.push({ { 0, 0 }, { 0, src } });
while
(!q.empty()) {
pair<pair<
int
,
int
>, pair<
int
,
int
> > FRONT = q.top();
q.pop();
int
cost = -FRONT.first.first;
int
stage = FRONT.first.second;
int
weight = FRONT.second.first;
int
v = FRONT.second.second;
shortest[v] = min(shortest[v], cost);
if
(shortest[dest] != MAXI)
break
;
if
(stage) {
for
(
int
i = _incr[v]; i < incr[v].size(); i++) {
if
(weight > incr[v][i].first)
q.push({ { -(cost + incr[v][i].first), 0 },
{ incr[v][i].first, incr[v][i].second } });
else
{
_incr[v] = i;
break
;
}
}
}
else
{
for
(
int
i = _decr[v]; i < decr[v].size(); i++) {
if
(weight < -decr[v][i].first)
q.push({ { -(cost - decr[v][i].first), 1 },
{ -decr[v][i].first, decr[v][i].second } });
else
{
_decr[v] = i;
break
;
}
}
}
}
if
(shortest[dest] == MAXI)
return
-1;
return
shortest[dest];
}
int
main()
{
n = 5, src = 4, dest = 3;
Add_edge(4, 2, 8);
Add_edge(1, 4, 6);
Add_edge(2, 3, 10);
Add_edge(3, 1, 10);
Add_edge(1, 2, 3);
Add_edge(3, 5, 3);
cout << Modified_Dijkstra();
return
0;
}