[Cpp]
# Include <iostream>
# Include <stdio. h>
# Include <cstring>
# Include <queue>
# Include <algorithm>
Using namespace std;
Const int maxn = 1001;
Const int maxm = 100001;
Const int inf = 1 <30;
Struct edge
{
Int from, to, next, w;
};
Edge e1 [maxm];
Edge e2 [maxm];
Int head1 [maxn];
Int head2 [maxn];
Int dist [maxn];
Bool vis [maxn];
Struct node // stores information about the midpoint in the search process
{
Int;
Int g, f; // f = g + h
Bool operator <(const node & r) const
{
If (r. f = f) return r. g <g;
Else return r. f <f;
}
};
Int n, m, t1, t2, k, cnt, s, end; // short circuit at k
Void add1 (int I, int j, int w)
{
E1 [t1]. from = I;
E1 [t1]. to = j;
E1 [t1]. w = w;
E1 [t1]. next = head1 [I];
Head1 [I] = t1 ++;
}
Void add2 (int I, int j, int w)
{
E2 [t2]. from = I;
E2 [t2]. to = j;
E2 [t2]. w = w;
E2 [t2]. next = head2 [I];
Head2 [I] = t2 ++;
}
Void spfa (int s) // obtain the shortest distance from the current value of the path to the end.
{
Queue <int> q;
Q. push (s );
For (int I = 1; I <= n; I ++) dist [I] = inf;
Memset (vis, false, sizeof (vis ));
Dist [s] = 0;
While (! Q. empty ())
{
Int u = q. front ();
Q. pop ();
Vis [u] = false; // use vis to mark whether a vertex is in the queue
For (int I = head2 [u]; I! =-1; I = e2 [I]. next)
{
Int v = e2 [I].;
If (dist [v]> dist [u] + e2 [I]. w)
{
Dist [v] = dist [u] + e2 [I]. w;
If (! Vis [v])
{
Q. push (v );
Vis [v] = true;
}
}
}
}
}
Int A (int s, int end, int k)
{
Int cnt = 0;
Node e, ne;
Priority_queue <node> que;
If (s = end) k ++; // Special Judgment
If (dist [s] = inf) return-1;
E. to = s;
E. f = e. g + dist [e. to]; // evaluate function formula
Que. push (e );
While (! Que. empty ())
{
E = que. top ();
Que. pop ();
If (e. to = end) cnt ++; // counts the number of times that the end node is outputted.
If (cnt = k) return e. g; // calculates the number of t queues. if the number of t pairs is k, the current path length g is counted as the k short circuit.
For (int I = head1 [e. to]; I! =-1; I = e1 [I]. next)
{
Ne. to = e1 [I].;
Ne. g = e. g + e1 [I]. w;
Ne. f = ne. g + dist [ne. to];
Que. push (ne );
}
}
Return-1;
}
Int main ()
{
Int u, v, w;
While (scanf ("% d", & n, & m) = 2)
{
Memset (head1,-1, sizeof (head1 ));
Memset (head2,-1, sizeof (head2 ));
Memset (e1, 0, sizeof (e1 ));
Memset (e2, 0, sizeof (e2 ));
T1 = t2 = 0;
For (int I = 1; I <= m; I ++)
{
Scanf ("% d", & u, & v, & w );
Add1 (u, v, w );
Add2 (v, u, w );
}
Scanf ("% d", & s, & end, & k );
Spfa (end );
Int ans = A (s, end, k );
Printf ("% d \ n", ans );
}
Return 0;
}