結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー Nzt3Nzt3
提出日時 2024-02-11 12:10:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 689 ms / 2,500 ms
コード長 1,236 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 212,688 KB
実行使用メモリ 43,904 KB
最終ジャッジ日時 2024-02-11 15:59:57
合計ジャッジ時間 11,386 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
12,672 KB
testcase_01 AC 347 ms
23,680 KB
testcase_02 AC 602 ms
40,192 KB
testcase_03 AC 3 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 3 ms
6,676 KB
testcase_06 AC 88 ms
8,192 KB
testcase_07 AC 568 ms
43,264 KB
testcase_08 AC 529 ms
38,784 KB
testcase_09 AC 568 ms
43,904 KB
testcase_10 AC 667 ms
43,904 KB
testcase_11 AC 689 ms
43,904 KB
testcase_12 AC 666 ms
43,904 KB
testcase_13 AC 689 ms
43,904 KB
testcase_14 AC 679 ms
43,904 KB
testcase_15 AC 82 ms
6,676 KB
testcase_16 AC 81 ms
6,676 KB
testcase_17 AC 82 ms
6,676 KB
testcase_18 AC 228 ms
14,848 KB
testcase_19 AC 430 ms
34,432 KB
testcase_20 AC 383 ms
28,800 KB
testcase_21 AC 499 ms
43,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;

constexpr int MAX_C=10;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N,M;
  cin>>N>>M;
  vector G(N,vector(0,0));
  for(int i=0;i<M;i++){
    int A,B;
    cin>>A>>B;
    --A,--B;
    G[A].push_back(B);
    G[B].push_back(A);
  }
  vector C(N,0);
  for(int &i:C)cin>>i,--i;
  vector W(MAX_C,0ll);
  for(ll &i:W)cin>>i;
  vector root(1<<MAX_C,vector(N,-1));
  for(int i=0;i<1<<MAX_C;i++){
    for(int j=0;j<N;j++){
      if(root[i][j]!=-1)continue;
      root[i][j]=j;
      if(!(i>>C[j]&1))continue;
      queue<int>bfs;
      bfs.push(j);
      while(bfs.size()){
        int v=bfs.front();
        bfs.pop();
        for(int k:G[v]){
          if((i>>C[k]&1)&&root[i][k]==-1){
            root[i][k]=j;
            bfs.push(k);
          }
        }
      }
    }
  }
  vector cost(1<<MAX_C,0ll);
  for(int i=0;i<1<<MAX_C;i++){
    for(int j=0;j<MAX_C;j++){
      if(i>>j&1)cost[i]+=W[j];
    }
  }
  int Q;
  cin>>Q;
  while(Q--){
    int U,V;
    cin>>U>>V;
    --U,--V;
    ll ans=1e12;
    for(int i=0;i<1<<MAX_C;i++){
      if(root[i][U]==root[i][V]){
        ans=min(ans,cost[i]);
      }
    }
    cout<<(ans==1e12?-1ll:ans)<<'\n';
  }
}
0