結果

問題 No.1480 Many Complete Graphs
ユーザー tentententen
提出日時 2023-02-02 13:03:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 823 ms / 2,000 ms
コード長 3,303 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 82,828 KB
実行使用メモリ 91,620 KB
最終ジャッジ日時 2023-09-14 19:39:34
合計ジャッジ時間 33,088 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,892 KB
testcase_01 AC 46 ms
49,564 KB
testcase_02 AC 47 ms
49,808 KB
testcase_03 AC 47 ms
49,644 KB
testcase_04 AC 47 ms
49,440 KB
testcase_05 AC 46 ms
49,364 KB
testcase_06 AC 46 ms
47,528 KB
testcase_07 AC 46 ms
49,408 KB
testcase_08 AC 50 ms
47,880 KB
testcase_09 AC 83 ms
52,088 KB
testcase_10 AC 84 ms
52,020 KB
testcase_11 AC 59 ms
49,848 KB
testcase_12 AC 69 ms
50,968 KB
testcase_13 AC 189 ms
60,796 KB
testcase_14 AC 161 ms
58,272 KB
testcase_15 AC 360 ms
64,204 KB
testcase_16 AC 251 ms
61,860 KB
testcase_17 AC 276 ms
61,964 KB
testcase_18 AC 146 ms
57,336 KB
testcase_19 AC 245 ms
62,044 KB
testcase_20 AC 319 ms
62,676 KB
testcase_21 AC 180 ms
61,688 KB
testcase_22 AC 148 ms
58,132 KB
testcase_23 AC 411 ms
74,408 KB
testcase_24 AC 457 ms
66,996 KB
testcase_25 AC 518 ms
71,824 KB
testcase_26 AC 564 ms
75,472 KB
testcase_27 AC 481 ms
66,964 KB
testcase_28 AC 542 ms
77,840 KB
testcase_29 AC 557 ms
78,136 KB
testcase_30 AC 536 ms
77,060 KB
testcase_31 AC 531 ms
77,444 KB
testcase_32 AC 552 ms
77,132 KB
testcase_33 AC 512 ms
77,252 KB
testcase_34 AC 525 ms
77,452 KB
testcase_35 AC 521 ms
77,016 KB
testcase_36 AC 516 ms
77,356 KB
testcase_37 AC 522 ms
77,580 KB
testcase_38 AC 525 ms
77,420 KB
testcase_39 AC 539 ms
77,768 KB
testcase_40 AC 526 ms
77,348 KB
testcase_41 AC 539 ms
77,472 KB
testcase_42 AC 532 ms
76,824 KB
testcase_43 AC 548 ms
77,332 KB
testcase_44 AC 527 ms
79,488 KB
testcase_45 AC 774 ms
80,100 KB
testcase_46 AC 519 ms
77,424 KB
testcase_47 AC 668 ms
81,332 KB
testcase_48 AC 688 ms
81,448 KB
testcase_49 AC 654 ms
81,596 KB
testcase_50 AC 400 ms
77,368 KB
testcase_51 AC 646 ms
81,348 KB
testcase_52 AC 823 ms
82,780 KB
testcase_53 AC 799 ms
80,388 KB
testcase_54 AC 679 ms
78,672 KB
testcase_55 AC 772 ms
82,884 KB
testcase_56 AC 797 ms
80,736 KB
testcase_57 AC 448 ms
83,268 KB
testcase_58 AC 814 ms
91,620 KB
evil_aftercontest.txt AC 897 ms
103,188 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();
                int idx = n + i * 2 + x % 2;
                graph.get(x - 1).add(new Path(idx, x / 2 + c));
                graph.get(idx).add(new Path(x - 1, x / 2 + x % 2));
            }
            graph.get(n + i * 2).add(new Path(n + i * 2 + 1, 0));
            graph.get(n + i * 2 + 1).add(new Path(n + i * 2, 1));
        }
        long[] costs = new long[n + m * 2];
        Arrays.fill(costs, Long.MAX_VALUE);
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0));
        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, x.value + p.value));
            }
        }
        if (costs[n - 1] == Long.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(costs[n - 1]);
        }
    }
    
    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