結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー tentententen
提出日時 2024-02-21 17:46:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 935 ms / 2,500 ms
コード長 4,013 bytes
コンパイル時間 7,577 ms
コンパイル使用メモリ 82,736 KB
実行使用メモリ 63,828 KB
最終ジャッジ日時 2024-02-21 17:47:06
合計ジャッジ時間 20,925 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 514 ms
62,748 KB
testcase_01 AC 788 ms
63,704 KB
testcase_02 AC 935 ms
63,148 KB
testcase_03 AC 97 ms
55,268 KB
testcase_04 AC 93 ms
55,164 KB
testcase_05 AC 96 ms
55,284 KB
testcase_06 AC 664 ms
63,300 KB
testcase_07 AC 873 ms
63,680 KB
testcase_08 AC 801 ms
63,828 KB
testcase_09 AC 762 ms
63,708 KB
testcase_10 AC 797 ms
63,320 KB
testcase_11 AC 835 ms
63,112 KB
testcase_12 AC 816 ms
63,128 KB
testcase_13 AC 809 ms
63,100 KB
testcase_14 AC 831 ms
63,504 KB
testcase_15 AC 737 ms
63,348 KB
testcase_16 AC 749 ms
63,604 KB
testcase_17 AC 750 ms
63,392 KB
testcase_18 AC 679 ms
62,832 KB
testcase_19 AC 720 ms
63,764 KB
testcase_20 AC 781 ms
63,576 KB
testcase_21 AC 754 ms
63,640 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