結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tentententen
提出日時 2020-12-17 21:10:01
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,620 bytes
コンパイル時間 4,495 ms
コンパイル使用メモリ 79,184 KB
実行使用メモリ 69,100 KB
最終ジャッジ日時 2023-10-21 07:08:43
合計ジャッジ時間 18,392 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
57,588 KB
testcase_01 AC 138 ms
55,780 KB
testcase_02 AC 134 ms
57,560 KB
testcase_03 AC 135 ms
57,452 KB
testcase_04 AC 136 ms
57,504 KB
testcase_05 AC 147 ms
57,588 KB
testcase_06 AC 149 ms
55,516 KB
testcase_07 AC 249 ms
60,856 KB
testcase_08 AC 255 ms
60,776 KB
testcase_09 AC 295 ms
63,780 KB
testcase_10 AC 274 ms
65,604 KB
testcase_11 AC 295 ms
64,132 KB
testcase_12 WA -
testcase_13 AC 277 ms
63,328 KB
testcase_14 AC 237 ms
62,560 KB
testcase_15 AC 271 ms
63,500 KB
testcase_16 WA -
testcase_17 AC 239 ms
60,580 KB
testcase_18 AC 236 ms
60,472 KB
testcase_19 AC 266 ms
61,204 KB
testcase_20 AC 220 ms
60,304 KB
testcase_21 WA -
testcase_22 AC 133 ms
57,644 KB
testcase_23 AC 138 ms
57,800 KB
testcase_24 AC 134 ms
57,672 KB
testcase_25 AC 135 ms
57,640 KB
testcase_26 AC 137 ms
57,432 KB
testcase_27 AC 135 ms
57,472 KB
testcase_28 AC 246 ms
62,736 KB
testcase_29 AC 285 ms
64,436 KB
testcase_30 AC 151 ms
57,516 KB
testcase_31 AC 237 ms
60,768 KB
testcase_32 AC 138 ms
55,508 KB
testcase_33 AC 286 ms
64,416 KB
testcase_34 AC 255 ms
61,040 KB
testcase_35 WA -
testcase_36 AC 217 ms
60,288 KB
testcase_37 AC 212 ms
57,880 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 146 ms
57,640 KB
testcase_41 AC 142 ms
57,692 KB
testcase_42 AC 133 ms
57,456 KB
testcase_43 AC 292 ms
63,528 KB
testcase_44 AC 256 ms
61,008 KB
testcase_45 AC 293 ms
63,824 KB
testcase_46 AC 240 ms
60,872 KB
testcase_47 AC 279 ms
64,216 KB
testcase_48 AC 281 ms
63,620 KB
testcase_49 AC 223 ms
59,820 KB
testcase_50 AC 140 ms
57,468 KB
testcase_51 AC 137 ms
57,616 KB
testcase_52 AC 235 ms
60,396 KB
testcase_53 AC 226 ms
60,244 KB
testcase_54 AC 341 ms
64,360 KB
testcase_55 AC 343 ms
66,272 KB
testcase_56 AC 341 ms
66,356 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static boolean isArrow;
    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);
        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 (!isArrow && y == from) {
                continue;
            }
            next(y, idx, x + graph.get(idx).get(y), costs);
        }
        costs[idx] = 0;
    }
}
0