結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー umezo
提出日時 2024-02-17 06:33:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,118 ms / 2,500 ms
コード長 1,624 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 205,044 KB
最終ジャッジ日時 2025-02-19 15:22:58
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;

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

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return root(x)==root(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(root(x),root(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int root(int x){
    if(x != p[x]) p[x]=root(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[root(x)];
  }
};

const ll INF=1e18;

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n,m;
  cin>>n>>m;
  vector<int> A(m),B(m);
  rep(i,m){
    cin>>A[i]>>B[i];
    A[i]--,B[i]--;
  }
  vector<int> C(n);
  rep(i,n){
    cin>>C[i];
    C[i]--;
  }
  vector<ll> W(10);
  rep(i,10) cin>>W[i];
  vector<Dis> ds(1<<10,n);
  vector<ll> cost(1<<10);
  for(int bit=1;bit<(1<<10);bit++){
    rep(i,10) if(bit&(1<<i)) cost[bit]+=W[i];
    rep(i,m){
      if((bit&(1<<C[A[i]])) && (bit&(1<<C[B[i]]))){
        ds[bit].unite(A[i],B[i]);
      } 
    }
  }
  int q;
  cin>>q;
  while(q--){
    int u,v;
    cin>>u>>v;
    u--,v--;
    ll ans=INF;
    rep(i,1<<10){
      if(ds[i].same(u,v)) ans=min(ans,cost[i]);
    }
    if(ans==INF) cout<<-1<<'\n';
    else cout<<ans<<'\n';
  }

  return 0;
}
0