結果

問題 No.1480 Many Complete Graphs
ユーザー tentententen
提出日時 2023-04-12 10:20:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 893 ms / 2,000 ms
コード長 3,278 bytes
コンパイル時間 3,873 ms
コンパイル使用メモリ 93,324 KB
実行使用メモリ 87,816 KB
最終ジャッジ日時 2024-04-17 00:28:11
合計ジャッジ時間 33,452 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
36,920 KB
testcase_01 AC 57 ms
36,920 KB
testcase_02 AC 58 ms
36,920 KB
testcase_03 AC 56 ms
37,320 KB
testcase_04 AC 57 ms
37,248 KB
testcase_05 AC 58 ms
37,160 KB
testcase_06 AC 59 ms
36,800 KB
testcase_07 AC 56 ms
36,872 KB
testcase_08 AC 58 ms
36,896 KB
testcase_09 AC 103 ms
39,016 KB
testcase_10 AC 106 ms
38,980 KB
testcase_11 AC 68 ms
37,436 KB
testcase_12 AC 86 ms
38,212 KB
testcase_13 AC 223 ms
50,304 KB
testcase_14 AC 187 ms
46,704 KB
testcase_15 AC 399 ms
53,956 KB
testcase_16 AC 294 ms
51,196 KB
testcase_17 AC 307 ms
51,256 KB
testcase_18 AC 169 ms
46,164 KB
testcase_19 AC 309 ms
51,312 KB
testcase_20 AC 382 ms
52,624 KB
testcase_21 AC 217 ms
50,104 KB
testcase_22 AC 170 ms
46,504 KB
testcase_23 AC 313 ms
63,424 KB
testcase_24 AC 501 ms
57,172 KB
testcase_25 AC 525 ms
61,604 KB
testcase_26 AC 599 ms
62,392 KB
testcase_27 AC 492 ms
56,760 KB
testcase_28 AC 590 ms
68,756 KB
testcase_29 AC 595 ms
66,516 KB
testcase_30 AC 585 ms
66,540 KB
testcase_31 AC 572 ms
66,724 KB
testcase_32 AC 579 ms
66,472 KB
testcase_33 AC 580 ms
66,300 KB
testcase_34 AC 568 ms
66,512 KB
testcase_35 AC 575 ms
66,652 KB
testcase_36 AC 571 ms
66,508 KB
testcase_37 AC 567 ms
66,292 KB
testcase_38 AC 574 ms
66,520 KB
testcase_39 AC 572 ms
66,656 KB
testcase_40 AC 576 ms
66,596 KB
testcase_41 AC 580 ms
66,784 KB
testcase_42 AC 584 ms
66,508 KB
testcase_43 AC 597 ms
69,196 KB
testcase_44 AC 600 ms
66,736 KB
testcase_45 AC 582 ms
66,764 KB
testcase_46 AC 580 ms
70,732 KB
testcase_47 AC 719 ms
70,920 KB
testcase_48 AC 765 ms
70,856 KB
testcase_49 AC 741 ms
80,300 KB
testcase_50 AC 468 ms
67,312 KB
testcase_51 AC 750 ms
70,968 KB
testcase_52 AC 708 ms
69,164 KB
testcase_53 AC 769 ms
72,080 KB
testcase_54 AC 784 ms
71,932 KB
testcase_55 AC 670 ms
69,024 KB
testcase_56 AC 793 ms
72,020 KB
testcase_57 AC 576 ms
87,816 KB
testcase_58 AC 893 ms
82,952 KB
evil_aftercontest.txt AC 970 ms
97,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<ArrayList<Path>> graph = new ArrayList<>();
        for (int i = 0; i < n + m * 2; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int k = sc.nextInt();
            int c = sc.nextInt();
            for (int j = 0; j < k; j++) {
                int x = sc.nextInt();
                graph.get(x - 1).add(new Path(n + i * 2 + x % 2, x + c));
                graph.get(n + i * 2 + x % 2).add(new Path(x - 1, x + c));
            }
            graph.get(n + i * 2).add(new Path(n + i * 2 + 1, 1));
            graph.get(n + i * 2 + 1).add(new Path(n + i * 2, 1));
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0));
        long[] costs = new long[n + m * 2];
        Arrays.fill(costs, Long.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            for (Path x : graph.get(p.idx)) {
                queue.add(new Path(x.idx, p.value + x.value));
            }
        }
        if (costs[n - 1] == Long.MAX_VALUE)  {
            System.out.println(-1);
        } else {
            System.out.println(costs[n - 1] / 2);
        }
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        long value;
        
        public Path(int idx, long value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0