Advertisement
vov44k

t178

Mar 9th, 2023
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class t178 {
  4.     static class edge {
  5.         int a, b, cost;
  6.  
  7.         public edge(int a, int b, int cost) {
  8.             this.a = a;
  9.             this.b = b;
  10.             this.cost = cost;
  11.         }
  12.     }
  13.  
  14.     static edge[] e;
  15.     static int[] d;
  16.     static final int t = 30000;
  17.  
  18.     public static void main(String[] args) {
  19.         Scanner in = new Scanner(System.in);
  20.         int n = in.nextInt();
  21.         int m = in.nextInt();
  22.         e = new edge[m + 1];
  23.         d = new int[n + 1];
  24.         for (int i = 1; i < m + 1; i++) {
  25.             e[i] = new edge(in.nextInt(), in.nextInt(), in.nextInt());
  26.         }
  27.         for (int i = 2; i < n + 1; i++) {
  28.             d[i] = t;
  29.         }
  30.         for (int i = 0; i < n - 1; i++) {
  31.             for (int j = 1; j < m + 1; j++) {
  32.                 if (d[e[j].a] < t) {
  33.                     d[e[j].b] = Math.min(d[e[j].b], d[e[j].a] + e[j].cost);
  34.                 }
  35.             }
  36.         }
  37.         for (int i = 1; i < n; i++) {
  38.             System.out.print(d[i] + " ");
  39.         }
  40.         System.out.println(d[n]);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement