結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー KoiKoi
提出日時 2024-02-16 23:11:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,062 bytes
コンパイル時間 2,215 ms
コンパイル使用メモリ 215,648 KB
実行使用メモリ 14,864 KB
最終ジャッジ日時 2024-02-16 23:13:04
合計ジャッジ時間 8,560 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 5 ms
14,184 KB
testcase_04 AC 5 ms
14,184 KB
testcase_05 AC 5 ms
14,184 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 487 ms
14,864 KB
testcase_16 AC 471 ms
14,864 KB
testcase_17 AC 489 ms
14,864 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
int N;
int M;
int C[10000];
int W[10000];
long long cost[1024];
vector<int> G[10000];
int Group[10000][1024];
int Answers[10000];
signed main(void){
    cin >> N >> M;
    for(int i = 0; i < 1024; i++){
        for(int j = 0; j < N; j++){
            Group[i][j] = -1;
        }
    }
    int a;
    int b;
    int c;
    for(int i = 0; i < M; i++){
        cin >> a >> b;
        G[a-1].push_back(b-1);
        G[b-1].push_back(a-1);
    }
    for(int i = 0; i < N; i++){
        cin >> c;
        C[i] = c;
    }
    for(int i = 0; i < 10; i++){
        cin >> c;
        W[i] = c;
    }
    for(int i = 0; i < 1024; i++){
        int T = 0;
        int w = 0;
        unordered_set<int> S={};
        int c = 0;
        for(int j = 0; j < 10; j++){
            if(i & (1<<j)){
                S.insert(j + 1);
                c+=W[j];
            }
        }
        cost[i] = c;
        while(T < N){
            queue<int> que={};
            que.push(T);
            Group[i][T] = w;
            while (!que.empty()){
                int p = que.front();
                que.pop();
                for(int q:G[p]){
                    if(S.count(C[p]) && S.count(C[q]) && Group[i][q] == -1){
                        Group[i][q] = Group[i][p];
                        que.push(q);
                    }
                }
            }
            while (Group[i][T] != -1){
                T+=1;
                if(T >= N){
                    break;
                }
            }
            w+=1;
        }
    }
    int Q;
    cin >> Q;
    int u;
    int v;
    for(int i = 0; i < Q; i++){
        long long ans = -1;
        cin >> u >> v;
        u-=1;
        v-=1;
        for(int j = 0; j < 1024; j++){
            if(Group[j][u]==Group[j][v]){
                if(ans == -1 || cost[j] < ans){
                    ans = cost[j];
                }
            }
        }
        Answers[i] = ans;
    }
    for(int i = 0; i < Q; i++){
        cout << Answers[i] << endl;
    }
}
0