結果
問題 | No.845 最長の切符 |
ユーザー |
|
提出日時 | 2019-06-28 22:37:25 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,129 bytes |
コンパイル時間 | 3,724 ms |
コンパイル使用メモリ | 79,228 KB |
実行使用メモリ | 44,456 KB |
最終ジャッジ日時 | 2024-07-02 04:59:02 |
合計ジャッジ時間 | 9,472 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 7 WA * 20 |
ソースコード
import java.io.*;import java.util.*;public class Main_yukicoder845 {private static Scanner sc;private static Printer pr;private static void solve() {int n = sc.nextInt();int m = sc.nextInt();FloydWarshall fw = new FloydWarshall(n);for (int i = 0; i < m; i++) {int a = sc.nextInt() - 1;int b = sc.nextInt() - 1;int c = sc.nextInt();if (fw.d[a][b] == fw.INF || c > fw.d[a][b]) {fw.addEdge(a, b, c);fw.addEdge(b, a, c);}}long max = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {long tmp = fw.getShortestPath(i, j);if (i != j && tmp != fw.INF) {max = Math.max(max, tmp);}}}pr.println(max);}// 全点対最短路 FloydWarshall O(V^3)private static class FloydWarshall {long[][] d;int n;long[][] result;boolean nf; // NEGATIVE CYCLE flagfinal long INF = Long.MAX_VALUE;FloydWarshall(int n) {this.n = n;d = new long[n][n];for (int i = 0; i < n; i++) {Arrays.fill(d[i], INF);d[i][i] = 0;}nf = false;}// i, j:0-indexedpublic void addEdge(int i, int j, int c) {d[i][j] = c;}// 負閉路があるときは-INFを返すpublic long getShortestPath(int i, int j) {if (nf) {return -INF;}if (result == null) {for (int kk = 0; kk < n; kk++) {for (int ii = 0; ii < n; ii++) {for (int jj = 0; jj < n; jj++) {// d[ii][jj] = Math.min(d[ii][jj], d[ii][kk] + d[kk][jj]);if (d[ii][kk] != INF && d[kk][jj] != INF && d[ii][jj] > d[ii][kk] + d[kk][jj]) {d[ii][jj] = d[ii][kk] + d[kk][jj];}}}}for (int k = 0; k < n; k++) {if (d[k][k] < 0) {nf = true;return -INF;}}result = d;}return result[i][j];}}// ---------------------------------------------------public static void main(String[] args) {sc = new Scanner(System.in);pr = new Printer(System.out);solve();pr.close();sc.close();}static class Printer extends PrintWriter {Printer(OutputStream out) {super(out);}}}