結果

問題 No.1480 Many Complete Graphs
ユーザー tentententen
提出日時 2023-06-22 16:19:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 807 ms / 2,000 ms
コード長 3,500 bytes
コンパイル時間 3,717 ms
コンパイル使用メモリ 81,272 KB
実行使用メモリ 93,932 KB
最終ジャッジ日時 2023-09-12 10:48:56
合計ジャッジ時間 33,101 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,504 KB
testcase_01 AC 44 ms
49,688 KB
testcase_02 AC 46 ms
49,612 KB
testcase_03 AC 44 ms
49,500 KB
testcase_04 AC 44 ms
49,664 KB
testcase_05 AC 44 ms
49,624 KB
testcase_06 AC 44 ms
49,708 KB
testcase_07 AC 44 ms
49,556 KB
testcase_08 AC 48 ms
49,632 KB
testcase_09 AC 81 ms
52,380 KB
testcase_10 AC 80 ms
52,292 KB
testcase_11 AC 56 ms
49,848 KB
testcase_12 AC 75 ms
51,380 KB
testcase_13 AC 179 ms
60,908 KB
testcase_14 AC 156 ms
58,804 KB
testcase_15 AC 326 ms
64,712 KB
testcase_16 AC 240 ms
62,416 KB
testcase_17 AC 261 ms
62,768 KB
testcase_18 AC 146 ms
58,168 KB
testcase_19 AC 251 ms
62,288 KB
testcase_20 AC 296 ms
62,428 KB
testcase_21 AC 173 ms
61,720 KB
testcase_22 AC 150 ms
58,388 KB
testcase_23 AC 401 ms
77,164 KB
testcase_24 AC 450 ms
67,212 KB
testcase_25 AC 488 ms
71,896 KB
testcase_26 AC 546 ms
75,024 KB
testcase_27 AC 465 ms
67,292 KB
testcase_28 AC 518 ms
77,724 KB
testcase_29 AC 533 ms
77,756 KB
testcase_30 AC 535 ms
77,560 KB
testcase_31 AC 545 ms
77,628 KB
testcase_32 AC 539 ms
77,436 KB
testcase_33 AC 523 ms
77,776 KB
testcase_34 AC 493 ms
76,732 KB
testcase_35 AC 526 ms
77,784 KB
testcase_36 AC 511 ms
77,600 KB
testcase_37 AC 526 ms
77,460 KB
testcase_38 AC 499 ms
77,080 KB
testcase_39 AC 522 ms
77,404 KB
testcase_40 AC 514 ms
77,224 KB
testcase_41 AC 500 ms
78,956 KB
testcase_42 AC 520 ms
77,656 KB
testcase_43 AC 507 ms
77,220 KB
testcase_44 AC 508 ms
77,588 KB
testcase_45 AC 522 ms
78,220 KB
testcase_46 AC 547 ms
77,828 KB
testcase_47 AC 617 ms
83,624 KB
testcase_48 AC 617 ms
81,736 KB
testcase_49 AC 653 ms
81,580 KB
testcase_50 AC 386 ms
77,432 KB
testcase_51 AC 643 ms
81,540 KB
testcase_52 AC 751 ms
81,460 KB
testcase_53 AC 747 ms
84,140 KB
testcase_54 AC 744 ms
81,980 KB
testcase_55 AC 734 ms
82,240 KB
testcase_56 AC 771 ms
85,756 KB
testcase_57 AC 452 ms
83,308 KB
testcase_58 AC 807 ms
93,932 KB
evil_aftercontest.txt AC 849 ms
105,880 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();
                if (x % 2 == 0) {
                    graph.get(x - 1).add(new Path(n + i * 2, x / 2 + c));
                    graph.get(n + i * 2).add(new Path(x - 1, x / 2));
                } else {
                    graph.get(x - 1).add(new Path(n + i * 2 + 1, x / 2 + c));
                    graph.get(n + i * 2 + 1).add(new Path(x - 1, x / 2 + 1));
                }
            }
            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));
        }
        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]);
        }
    }

    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