#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; const int maxn = 1000 + 10; const int INF = 1<<29; vector<int>E[maxn], W[maxn], Ek[maxn], Wk[maxn]; int d[maxn]; bool vis[maxn]; struct Node { int u, len; Node(int tu, int tlen) : u(tu), len(tlen) {} Node(){} bool operator < (const Node & rhs) const { return len + d[u] > rhs.len + d[rhs.u]; } }; int k_road(int s, int t, int n, int k) { priority_queue<Node>q; q.push(Node(s, 0)); int tk = 0; while(!q.empty()) { Node f = q.top(); q.pop(); if(f.u == t) { tk++; if(tk == k) { return f.len; } } int u = f.u; for( int i=0; i<Ek[u].size(); i++ ) { int v = Ek[u][i]; q.push(Node(v, f.len+Wk[u][i])); } } return -1; } void spfa(int s, int t, int n) { for( int i=1; i<=n; i++ ) d[i] = INF; d[t] = 0; queue<int>q; q.push(t); memset(vis, 0, sizeof(vis)); vis[t] = true; while(!q.empty()) { int f = q.front(); q.pop(); vis[f] = false; for( int i=0; i<E[f].size(); i++ ) { int v = E[f][i]; if(d[v] > d[f] + W[f][i]) { d[v] = d[f] + W[f][i]; if(vis[v] == false) { vis[v] = true; q.push(v); } } } } } int main() { //freopen("in","r", stdin); int n, m; while(scanf("%d%d", &n, &m) != EOF) { int s, t, k; for( int i=1; i<=n; i++ ) E[i].clear(), W[i].clear(), Ek[i].clear(), Wk[i].clear(); for( int i=0; i<m; i++ ) { int u, v, w; scanf("%d%d%d", &v, &u, &w); E[u].push_back(v); W[u].push_back(w); Ek[v].push_back(u); Wk[v].push_back(w); } scanf("%d%d%d", &s, &t, &k); if(s == t) k++; spfa(s, t, n); if(d[s] == INF) { printf("-1\n"); continue; } /*for( int i=1; i<=n; i++ ) printf("%d ", d[i]); printf("\n");*/ int ans = k_road(s, t, n, k); if(ans == -1) printf("-1\n"); else printf("%d\n", ans); } return 0; }