Advertisement
vov44k

t1995

Mar 9th, 2023
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class t1995 {
  5.  
  6.     FastScanner in;
  7.     PrintWriter out;
  8.  
  9.     class Edge{
  10.         int v, u;
  11.         long w;
  12.         Edge(int v, int u, long w){
  13.             this.v = v;
  14.             this.u = u;
  15.             this.w = w;
  16.         }
  17.     }
  18.  
  19.     private void solve() throws IOException {
  20.         int n = in.nextInt(), m = in.nextInt();
  21.         int k = in.nextInt(), s = in.nextInt() - 1, f = in.nextInt() - 1;
  22.         Edge[] a = new Edge[m];
  23.         for(int i = 0; i < m; i++){
  24.             a[i] = new Edge(in.nextInt() - 1, in.nextInt() - 1, in.nextLong());
  25.         }
  26.         long[][] dist = new long[n][k + 1];
  27.         for(int i = 0; i < n; i++){
  28.             for(int j = 0; j < k + 1; j++){
  29.                 dist[i][j] = Integer.MAX_VALUE;
  30.             }
  31.         }
  32.         dist[s][0] = 0;
  33.         for(int i = 0; i < k; i++){
  34.             for(Edge j : a){
  35.                 dist[j.u][i + 1] = Math.min(dist[j.u][i + 1], dist[j.u][i]);
  36.                 if(dist[j.u][i + 1] > dist[j.v][i] + j.w){
  37.                     dist[j.u][i + 1] = dist[j.v][i] + j.w;
  38.                 }
  39.             }
  40.         }
  41.         out.print(dist[f][k] >= Integer.MAX_VALUE ? -1 : dist[f][k]);
  42.  
  43.     }
  44.  
  45.  
  46.  
  47.     class FastScanner {
  48.         StringTokenizer st;
  49.         BufferedReader br;
  50.  
  51.         FastScanner(InputStream s) {
  52.             br = new BufferedReader(new InputStreamReader(s));
  53.         }
  54.  
  55.         String next() throws IOException {
  56.             while (st == null || !st.hasMoreTokens()) {
  57.                 st = new StringTokenizer(br.readLine());
  58.             }
  59.             return st.nextToken();
  60.         }
  61.  
  62.         boolean hasNext() throws IOException {
  63.             return br.ready() || (st != null && st.hasMoreTokens());
  64.         }
  65.  
  66.         int nextInt() throws IOException {
  67.             return Integer.parseInt(next());
  68.         }
  69.  
  70.         long nextLong() throws IOException {
  71.             return Long.parseLong(next());
  72.         }
  73.  
  74.         double nextDouble() throws IOException {
  75.             return Double.parseDouble(next().replace(',', '.'));
  76.         }
  77.  
  78.         boolean hasNextLine() throws IOException {
  79.             return br.ready();
  80.         }
  81.  
  82.         String nextLine() throws IOException {
  83.             return br.readLine();
  84.         }
  85.  
  86.     }
  87.  
  88.     private void run() throws IOException {
  89.         in = new FastScanner(System.in); //new FileInputStream(".in");
  90.         out = new PrintWriter(System.out);
  91.  
  92.         solve();
  93.         out.flush();
  94.         out.close();
  95.     }
  96.  
  97.  
  98.     public static void main(String[] args) throws IOException {
  99.         new Main().run();
  100.  
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement