結果

問題 No.1344 Typical Shortest Path Sum
ユーザー tenten
提出日時 2022-08-17 13:18:44
言語 Java
(openjdk 23)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 2,033 bytes
コンパイル時間 2,964 ms
コンパイル使用メモリ 78,116 KB
実行使用メモリ 52,860 KB
最終ジャッジ日時 2024-10-04 14:16:03
合計ジャッジ時間 8,784 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 77
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        long[][] costs = new long[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(costs[i], Long.MAX_VALUE / 2);
            costs[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int left = sc.nextInt() - 1;
            int right = sc.nextInt() - 1;
            long v = sc.nextLong();
            costs[left][right] = Math.min(costs[left][right], v);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    if (costs[j][i] < Long.MAX_VALUE / 2 && costs[i][k] < Long.MAX_VALUE / 2) {
                        costs[j][k] = Math.min(costs[j][k], costs[j][i] + costs[i][k]);
                    }
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            long sum = 0;
            for (long x : costs[i]) {
                if (x < Long.MAX_VALUE / 2) {
                    sum += x;
                }
            }
            sb.append(sum).append("\n");
        }
        System.out.print(sb);
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0