結果
問題 | No.17 2つの地点に泊まりたい |
ユーザー | Grenache |
提出日時 | 2015-11-03 15:30:08 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 96 ms / 5,000 ms |
コード長 | 3,155 bytes |
コンパイル時間 | 3,617 ms |
コンパイル使用メモリ | 78,608 KB |
実行使用メモリ | 38,536 KB |
最終ジャッジ日時 | 2024-10-15 01:28:50 |
合計ジャッジ時間 | 6,161 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
36,712 KB |
testcase_01 | AC | 50 ms
36,768 KB |
testcase_02 | AC | 51 ms
36,984 KB |
testcase_03 | AC | 58 ms
36,924 KB |
testcase_04 | AC | 55 ms
36,984 KB |
testcase_05 | AC | 79 ms
37,736 KB |
testcase_06 | AC | 72 ms
37,624 KB |
testcase_07 | AC | 69 ms
37,876 KB |
testcase_08 | AC | 91 ms
38,536 KB |
testcase_09 | AC | 96 ms
38,392 KB |
testcase_10 | AC | 64 ms
36,948 KB |
testcase_11 | AC | 89 ms
38,384 KB |
testcase_12 | AC | 51 ms
36,768 KB |
testcase_13 | AC | 50 ms
36,852 KB |
testcase_14 | AC | 49 ms
36,728 KB |
testcase_15 | AC | 51 ms
36,892 KB |
testcase_16 | AC | 50 ms
36,824 KB |
testcase_17 | AC | 51 ms
36,696 KB |
testcase_18 | AC | 54 ms
37,000 KB |
testcase_19 | AC | 53 ms
36,600 KB |
testcase_20 | AC | 49 ms
36,732 KB |
testcase_21 | AC | 53 ms
36,600 KB |
testcase_22 | AC | 57 ms
36,976 KB |
testcase_23 | AC | 77 ms
38,196 KB |
testcase_24 | AC | 77 ms
38,024 KB |
testcase_25 | AC | 54 ms
36,484 KB |
testcase_26 | AC | 93 ms
38,284 KB |
ソースコード
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Iterator; public class Main_yukicoder17 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); int n = sc.nextInt(); int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = sc.nextInt(); } FloydWarshall fw = new FloydWarshall(n); int m = sc.nextInt(); for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); fw.addEdge(a, b, c); fw.addEdge(b, a, c); } long min = Long.MAX_VALUE; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { if (i == j) { continue; } long tmp = fw.getShortestPath(0, i) + fw.getShortestPath(i, j) + fw.getShortestPath(j, n - 1); tmp += s[i] + s[j]; min = Math.min(min, tmp); } } pr.println(min); pr.close(); sc.close(); } private static class FloydWarshall { long[][] d; int n; long[][] result; boolean nf; // NEGATIVE CYCLE flag final static long INF = Long.MAX_VALUE / 4; 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-indexed public void addEdge(int i, int j, int c) { d[i][j] = c; } 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]; } } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator<String> it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }