結果
問題 | No.2630 Colorful Vertices and Cheapest Paths |
ユーザー | otoshigo |
提出日時 | 2024-02-16 21:59:50 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 726 ms / 2,500 ms |
コード長 | 2,127 bytes |
コンパイル時間 | 781 ms |
コンパイル使用メモリ | 85,468 KB |
実行使用メモリ | 83,456 KB |
最終ジャッジ日時 | 2024-09-28 20:17:19 |
合計ジャッジ時間 | 10,209 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#include<iostream> #include<algorithm> #include<vector> using namespace std; using ll=long long; #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} struct UnionFind { private: vector<int> par, siz; public: UnionFind(){} UnionFind(int n) : par(n, -1), siz(n, 1) {} int find(int x) { if (par[x] == -1) return x; else { par[x] = find(par[x]); return par[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return siz[find(x)]; } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin>>N>>M; vector<int>A(M),B(M); for(int i=0;i<M;i++){ cin>>A[i]>>B[i]; A[i]--; B[i]--; } vector<int>C(N); for(int i=0;i<N;i++){ cin>>C[i]; C[i]--; } vector<ll>W(10); for(int i=0;i<10;i++)cin>>W[i]; int Q; cin>>Q; vector<UnionFind>ufs(1<<10); for(int i=0;i<1<<10;i++){ UnionFind uf(N); ufs[i]=uf; } for(int bit=0;bit<1<<10;bit++){ for(int i=0;i<M;i++){ if((bit>>C[A[i]]&1)&&(bit>>C[B[i]]&1)){ ufs[bit].merge(A[i],B[i]); } } } vector<ll>Bs(1<<10); for(int bit=0;bit<1<<10;bit++){ for(int i=0;i<10;i++){ if(bit>>i&1)Bs[bit]+=W[i]; } } while(Q--){ int u,v; cin>>u>>v; u--; v--; ll ans=1LL<<60; for(int bit=0;bit<1<<10;bit++){ if(ufs[bit].same(u,v)){ chmin(ans,Bs[bit]); } } if(ans==1LL<<60)cout<<-1<<"\n"; else cout<<ans<<"\n"; } }