結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー tentententen
提出日時 2024-02-21 17:46:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 811 ms / 2,500 ms
コード長 4,013 bytes
コンパイル時間 2,695 ms
コンパイル使用メモリ 85,764 KB
実行使用メモリ 60,304 KB
最終ジャッジ日時 2024-09-29 04:17:36
合計ジャッジ時間 19,250 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 439 ms
59,144 KB
testcase_01 AC 697 ms
59,968 KB
testcase_02 AC 787 ms
59,404 KB
testcase_03 AC 91 ms
51,440 KB
testcase_04 AC 93 ms
51,300 KB
testcase_05 AC 90 ms
51,424 KB
testcase_06 AC 610 ms
59,520 KB
testcase_07 AC 783 ms
59,612 KB
testcase_08 AC 715 ms
59,960 KB
testcase_09 AC 705 ms
59,752 KB
testcase_10 AC 799 ms
59,612 KB
testcase_11 AC 768 ms
59,348 KB
testcase_12 AC 811 ms
59,708 KB
testcase_13 AC 808 ms
59,380 KB
testcase_14 AC 790 ms
59,356 KB
testcase_15 AC 701 ms
59,848 KB
testcase_16 AC 713 ms
59,876 KB
testcase_17 AC 680 ms
59,844 KB
testcase_18 AC 660 ms
59,932 KB
testcase_19 AC 681 ms
60,060 KB
testcase_20 AC 744 ms
59,808 KB
testcase_21 AC 709 ms
60,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static List<Set<Integer>> graph;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        Road[] roads = new Road[m];
        for (int i = 0; i < m; i++) {
            roads[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1);
        }
        int[] colors = new int[n];
        for (int i = 0; i < n; i++) {
            colors[i] = sc.nextInt() - 1;
        }
        int[] values = new int[10];
        for (int i = 0; i < 10; i++) {
            values[i] = sc.nextInt();
        }
        int[] mask = new int[m];
        for (int i = 0; i < m; i++) {
            mask[i] = (1 << colors[roads[i].left]) | (1 << colors[roads[i].right]);
        }
        int q = sc.nextInt();
        Road[] queries = new Road[q];
        for (int i = 0; i < q; i++) {
            queries[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1);
        }
        long[] ans = new long[q];
        Arrays.fill(ans, Long.MAX_VALUE);
        UnionFindTree uft = new UnionFindTree(n);
        long[] dp = new long[1 << 10];
        for (int i = 1; i < (1 << 10); i++) {
            final int d = i;
            int idx = IntStream.range(0, 10).filter(j -> (d & (1 << j)) > 0).findFirst().orElse(0);
            dp[d] = dp[d ^ (1 << idx)] + values[idx];
            uft.clear();
            for (int j = 0; j < m; j++) {
                if ((d & (mask[j])) != mask[j]) {
                    continue;
                }
                uft.unite(roads[j]);
            }
            for (int j = 0; j < q; j++) {
                if (uft.same(queries[j])) {
                    ans[j] = Math.min(ans[j], dp[d]);
                }
            }
        }
        System.out.println(Arrays.stream(ans).mapToObj(i -> String.valueOf(i == Long.MAX_VALUE ? -1 : i)).collect(Collectors.joining("\n")));
    }
    
    static class Road {
        int left;
        int right;
        
        public Road(int left, int right) {
            this.left = left;
            this.right = right;
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
        }
        
        public void clear() {
            for (int i = 0; i < parents.length; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (parents[x]  == x) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean same(Road r) {
            return same(r.left, r.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public void unite(Road r) {
            unite(r.left, r.right);
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0