結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tentententen
提出日時 2020-12-17 21:03:18
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,588 bytes
コンパイル時間 2,922 ms
コンパイル使用メモリ 82,896 KB
実行使用メモリ 69,152 KB
最終ジャッジ日時 2023-10-21 07:08:01
合計ジャッジ時間 16,165 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
56,280 KB
testcase_01 AC 124 ms
57,448 KB
testcase_02 AC 118 ms
57,556 KB
testcase_03 AC 124 ms
57,524 KB
testcase_04 AC 116 ms
57,368 KB
testcase_05 AC 117 ms
56,892 KB
testcase_06 AC 131 ms
57,492 KB
testcase_07 AC 214 ms
60,816 KB
testcase_08 AC 219 ms
60,840 KB
testcase_09 AC 251 ms
64,284 KB
testcase_10 AC 226 ms
65,268 KB
testcase_11 AC 244 ms
63,820 KB
testcase_12 WA -
testcase_13 AC 244 ms
63,272 KB
testcase_14 AC 215 ms
62,904 KB
testcase_15 AC 235 ms
61,928 KB
testcase_16 WA -
testcase_17 AC 215 ms
60,568 KB
testcase_18 AC 204 ms
60,684 KB
testcase_19 AC 230 ms
61,152 KB
testcase_20 AC 199 ms
58,388 KB
testcase_21 WA -
testcase_22 AC 101 ms
56,056 KB
testcase_23 AC 117 ms
57,708 KB
testcase_24 AC 111 ms
57,148 KB
testcase_25 AC 118 ms
57,656 KB
testcase_26 AC 122 ms
57,496 KB
testcase_27 AC 128 ms
57,524 KB
testcase_28 AC 229 ms
62,544 KB
testcase_29 AC 255 ms
64,472 KB
testcase_30 AC 133 ms
57,556 KB
testcase_31 AC 199 ms
60,384 KB
testcase_32 AC 122 ms
57,636 KB
testcase_33 AC 261 ms
64,272 KB
testcase_34 AC 223 ms
61,132 KB
testcase_35 WA -
testcase_36 AC 206 ms
60,216 KB
testcase_37 AC 176 ms
55,676 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 129 ms
57,436 KB
testcase_41 AC 124 ms
57,756 KB
testcase_42 AC 119 ms
57,328 KB
testcase_43 AC 254 ms
64,148 KB
testcase_44 AC 229 ms
60,836 KB
testcase_45 AC 272 ms
64,120 KB
testcase_46 AC 210 ms
61,268 KB
testcase_47 AC 251 ms
64,752 KB
testcase_48 AC 246 ms
64,040 KB
testcase_49 AC 194 ms
60,608 KB
testcase_50 AC 131 ms
57,720 KB
testcase_51 AC 116 ms
57,508 KB
testcase_52 AC 195 ms
60,308 KB
testcase_53 AC 195 ms
60,384 KB
testcase_54 AC 295 ms
66,624 KB
testcase_55 AC 311 ms
64,364 KB
testcase_56 AC 296 ms
66,376 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static boolean[][] used;
    static long min = Long.MAX_VALUE;
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean isArrow = (sc.nextInt() == 1);
        int n = sc.nextInt();
        int m = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).put(b, c);
            if (!isArrow) {
                graph.get(b).put(a, c);
            }
        }
        used = new boolean[n][n];
        long[] costs = new long[n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(costs, 0);
            next(i, i, 1, costs);
        }
        if (min == Long.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
    
    static void next(int idx, int from, long x, long[] costs) {
        if (costs[idx] != 0) {
            min = Math.min(min, x - costs[idx]);
            return;
        }
        if (used[from][idx]) {
            return;
        }
        used[from][idx] = true;
        costs[idx] = x;
        for (int y : graph.get(idx).keySet()) {
            if (y == from) {
                continue;
            }
            next(y, idx, x + graph.get(idx).get(y), costs);
        }
        costs[idx] = 0;
    }
}
0