Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class t1995 {
- FastScanner in;
- PrintWriter out;
- class Edge{
- int v, u;
- long w;
- Edge(int v, int u, long w){
- this.v = v;
- this.u = u;
- this.w = w;
- }
- }
- private void solve() throws IOException {
- int n = in.nextInt(), m = in.nextInt();
- int k = in.nextInt(), s = in.nextInt() - 1, f = in.nextInt() - 1;
- Edge[] a = new Edge[m];
- for(int i = 0; i < m; i++){
- a[i] = new Edge(in.nextInt() - 1, in.nextInt() - 1, in.nextLong());
- }
- long[][] dist = new long[n][k + 1];
- for(int i = 0; i < n; i++){
- for(int j = 0; j < k + 1; j++){
- dist[i][j] = Integer.MAX_VALUE;
- }
- }
- dist[s][0] = 0;
- for(int i = 0; i < k; i++){
- for(Edge j : a){
- dist[j.u][i + 1] = Math.min(dist[j.u][i + 1], dist[j.u][i]);
- if(dist[j.u][i + 1] > dist[j.v][i] + j.w){
- dist[j.u][i + 1] = dist[j.v][i] + j.w;
- }
- }
- }
- out.print(dist[f][k] >= Integer.MAX_VALUE ? -1 : dist[f][k]);
- }
- class FastScanner {
- StringTokenizer st;
- BufferedReader br;
- FastScanner(InputStream s) {
- br = new BufferedReader(new InputStreamReader(s));
- }
- String next() throws IOException {
- while (st == null || !st.hasMoreTokens()) {
- st = new StringTokenizer(br.readLine());
- }
- return st.nextToken();
- }
- boolean hasNext() throws IOException {
- return br.ready() || (st != null && st.hasMoreTokens());
- }
- int nextInt() throws IOException {
- return Integer.parseInt(next());
- }
- long nextLong() throws IOException {
- return Long.parseLong(next());
- }
- double nextDouble() throws IOException {
- return Double.parseDouble(next().replace(',', '.'));
- }
- boolean hasNextLine() throws IOException {
- return br.ready();
- }
- String nextLine() throws IOException {
- return br.readLine();
- }
- }
- private void run() throws IOException {
- in = new FastScanner(System.in); //new FileInputStream(".in");
- out = new PrintWriter(System.out);
- solve();
- out.flush();
- out.close();
- }
- public static void main(String[] args) throws IOException {
- new Main().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement