Faint child
[Html]
# Include <cstdio>
# Include <cstring>
# Define N 100010
# Define M 400010
# Define INF 999999999
Int n, m, s, a, B;
Int head [N], cnt;
Struct Edge
{
Int v, next, w;
} Edge [M];
Struct Heap
{
Int d, v, p;
} Heap [N];
Int hl, pos [N];
Void addedge (int u, int v, int w)
{
Edge [cnt]. next = head [u]; edge [cnt]. v = v; edge [cnt]. w = w;
Head [u] = cnt ++;
}
Void swap (int a, int B)
{
Heap [0] = heap [a]; heap [a] = heap [B]; heap [B] = heap [0];
Pos [heap [a]. v] = a; pos [heap [B]. v] = B;
}
Void heapfy ()
{
Int I = 2;
While (I <= hl)
{
If (I + 1 <= hl & heap [I + 1]. d If (heap [I]. d {
Swap (I, I> 1 );
I <= 1 ;;
}
Else break;
}
}
Void decrease (int I)
{
While (I! = 1 & heap [I]. d {
Swap (I, I> 1); I >>= 1;
}
}
Void relax (int u, int v, int w)
{
If (heap [pos [u]. d + w {
Heap [pos [v]. d = heap [pos [u]. d + w;
Decrease (pos [v]);
}
}
Void dijkstra (int s, int n)
{
Int u, I;
For (I = 1; I <= n; I ++)
{
Heap [I]. v = pos [I] = I; heap [I]. d = INF;
}
Heap [s]. p = s; heap [s]. d = 0; swap (1, s); hl = n;
While (hl)
{
U = heap [1]. v;
Swap (1, hl );
Hl --;
Heapfy ();
For (I = head [u]; I! =-1; I = edge [I]. next)
If (pos [edge [I]. v] <= hl) relax (u, edge [I]. v, edge [I]. w );
}
}
Int main ()
{
Int ds1, ds2, dab;
While (scanf ("% d", & m, & n, & s, & a, & B )! =-1)
{
Cnt = 0;
Memset (head,-1, sizeof (head ));
While (m --)
{
Int u, v, w;
Scanf ("% d", & u, & v, & w );
Addedge (u, v, w );
Addedge (v, u, w );
}
Dijkstra (s, n );
Ds1 = heap [pos [a]. d;
Ds2 = heap [pos [B]. d;
Dijkstra (a, n );
Dab = heap [pos [B]. d;
Printf ("% d \ n", dab + (ds1 <ds2? Ds1: ds2 ));
}
Return 0;
}