結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー GOTKAKOGOTKAKO
提出日時 2024-02-16 22:24:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,814 bytes
コンパイル時間 2,394 ms
コンパイル使用メモリ 216,836 KB
実行使用メモリ 16,936 KB
最終ジャッジ日時 2024-02-16 22:24:57
合計ジャッジ時間 9,994 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

class UnionFind{
    public:
    vector<int> par,siz;
 
    void make(int N){
        par.resize(N,-1);
        siz.resize(N,1);
    }
 
    int root(int x){
        if(par.at(x) == -1) return x;
        else return par.at(x) = root(par.at(x));
    }
 
    void unite(int u, int v){
        u = root(u),v = root(v);
        if(u == v) return;
        if(siz.at(u) < siz.at(v)) swap(u,v);
 
        par.at(v) = u;
        siz.at(u) += siz.at(v);
    }
 
    bool issame(int u, int v){
        if(root(u) == root(v)) return true;
        else return false;
    }
};

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

    int N,M; cin >> N >> M;
    UnionFind Z; Z.make(N);
    vector<vector<int>> Graph(N);
    for(int i=0; i<M; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Z.unite(u,v);
        Graph.at(u).push_back(v);
        Graph.at(v).push_back(u);
    }
    vector<int> C(N);
    vector<long long> W(10);
    for(auto &c : C) cin >> c,c--;
    for(auto &w : W) cin >> w;

    int Q; cin >> Q;
    for(int i=0; i<Q; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        if(Z.issame(u,v) == false){cout << -1 << endl; return 0;}

        long long now = 1e18;
        vector<bool> visited(N);
    auto dfs = [&](auto dfs,int pos,int W2,long long cost) -> void {
        if(pos == v){now = min(cost,now); return;}
        if(cost >= now) return;

        visited.at(pos) = true;
        for(auto to : Graph.at(pos)){
            if(visited.at(to)) continue;
            if(W2&(1<<C.at(to))) dfs(dfs,to,W2,cost);
            else dfs(dfs,to,W2+(1<<C.at(to)),cost+W.at(C.at(to)));
        }
        visited.at(pos) = false;
    }; 
        dfs(dfs,u,1<<C.at(u),W.at(C.at(u)));
        cout << now << endl;
    }
}
0