結果

問題 No.17 2つの地点に泊まりたい
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-04 01:48:19
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,237 bytes
コンパイル時間 3,782 ms
コンパイル使用メモリ 75,188 KB
実行使用メモリ 59,496 KB
最終ジャッジ日時 2023-09-17 04:02:45
合計ジャッジ時間 9,641 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
56,016 KB
testcase_01 AC 125 ms
55,692 KB
testcase_02 AC 131 ms
56,136 KB
testcase_03 AC 163 ms
56,580 KB
testcase_04 AC 183 ms
56,500 KB
testcase_05 AC 205 ms
58,920 KB
testcase_06 AC 189 ms
58,808 KB
testcase_07 AC 194 ms
58,552 KB
testcase_08 AC 207 ms
59,208 KB
testcase_09 AC 214 ms
58,884 KB
testcase_10 AC 194 ms
56,656 KB
testcase_11 AC 201 ms
58,832 KB
testcase_12 AC 125 ms
55,548 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 201 ms
58,644 KB
testcase_24 AC 203 ms
59,496 KB
testcase_25 AC 138 ms
55,764 KB
testcase_26 AC 206 ms
58,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// No.17 2つの地点に泊まりたい

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No17 {
    
    static final Scanner sc = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i=0; i<n; i++) a[i] = sc.nextInt();
        int m = sc.nextInt();
        int[][] g = new int[n][n];
        for (int i=0; i<n; i++) fill(g[i],Integer.MAX_VALUE/2);
        for (int i=0; i<m; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int c = sc.nextInt();
            g[s][t] = c; g[t][s] = c;
        }
        int[][] d = warshallFloyd(g);
        int min = Integer.MAX_VALUE;
        for (int i=1; i<n-1; i++) {
            for (int j=i+1; j<n-1; j++) {
                min = min(min,min(a[i]+a[j]+d[0][i]+d[i][j]+d[j][n-1],a[i]+a[j]+d[0][j]+d[j][i]+d[i][n-1]));
            }
        }
        out.println(min);
    }

    static int[][] warshallFloyd(int[][] g) {
        int n = g.length;
        int[][] d = g.clone();
        for (int k=0; k<n; k++)
            for (int i=0; i<n; i++)
                for (int j=0; j<n; j++)
                    d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
        return d;
    }

    static int[][][] toAdjacencyList(int n,int[] from, int[] to, int[] cost) {
        int[] cnt = new int[n];
        for (int i : from) cnt[i]++;
        for (int i : to) cnt[i]++;
    
        int[][][] g = new int[n][][];
        for (int i=0; i<n; i++) g[i] = new int[cnt[i]][2];
        for (int i=0; i<from.length; i++) {
            int f = from[i];
            int t = to[i];
            g[f][--cnt[f]][0] = t;
            g[t][--cnt[t]][0] = f;
            g[f][cnt[f]][1] = g[t][cnt[t]][1] = cost[i];
        }
    
        return g;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        sc.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}
0