結果

問題 No.114 遠い未来
ユーザー t98slidert98slider
提出日時 2022-06-17 02:12:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,714 ms / 5,000 ms
コード長 5,011 bytes
コンパイル時間 2,754 ms
コンパイル使用メモリ 195,356 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2024-04-16 16:08:04
合計ジャッジ時間 17,754 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
5,248 KB
testcase_01 AC 1,463 ms
9,216 KB
testcase_02 AC 272 ms
5,376 KB
testcase_03 AC 57 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 7 ms
5,376 KB
testcase_06 AC 800 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 162 ms
5,376 KB
testcase_11 AC 469 ms
6,144 KB
testcase_12 AC 1,460 ms
9,088 KB
testcase_13 AC 1,483 ms
9,216 KB
testcase_14 AC 761 ms
5,376 KB
testcase_15 AC 3,714 ms
5,376 KB
testcase_16 AC 382 ms
5,376 KB
testcase_17 AC 392 ms
5,376 KB
testcase_18 AC 2,041 ms
5,376 KB
testcase_19 AC 195 ms
5,376 KB
testcase_20 AC 243 ms
5,376 KB
testcase_21 AC 13 ms
5,376 KB
testcase_22 AC 15 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

template<typename T> struct steiner_tree{
    int N;
    std::vector<std::vector<std::pair<int, T>>> G;
    std::vector<std::vector<T>> dp;
    steiner_tree(int node_size) : N(node_size), G(node_size){}
    void add_edge(int u, int v, T cost){
        assert(0 <= u && u < N);
        assert(0 <= v && v < N);
        G[u].emplace_back(v, cost);
        G[v].emplace_back(u, cost);
    }
    T solve(std::vector<int> &terminal){
        int t = terminal.size();
        if(t == 0)return T(0);
        dp.resize(1 << t, std::vector<T>(N, std::numeric_limits<T>::max() / 2));
        for(int i = 0; i < t; i++){
            assert(0 <= terminal[i] && terminal[i] < N);
            dp[1 << i][terminal[i]] = 0;
        }
        for(int i = 1; i < (1 << t); i++){
            for(int j = 0; j < N; j++){
                for(int k = i; k > 0; k = (k - 1) & i){
                    dp[i][j] = std::min(dp[i][j], dp[k][j] + dp[i ^ k][j]);
                }
            }
            if(i == (1 << t) - 1) break;
            std::priority_queue<std::pair<T, int>, 
                    std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> pq;
            for(int j = 0; j < N; j++){
                pq.push(make_pair(dp[i][j], j));
            }
            T d;
            int v;
            while(!pq.empty()){
                std::tie(d, v) = pq.top();
                pq.pop();
                if(d > dp[i][v])continue;
                for(int j = 0, to, cost; j < G[v].size(); j++){
                    std::tie(to, cost) = G[v][j];
                    if(d + cost >= dp[i][to])continue;
                    dp[i][to] = d + cost;
                    pq.push(make_pair(dp[i][to], to));
                }
            }
        }
        return dp.back()[terminal[0]];
    }
};

int main(){
    int N, M, T;
    cin >> N >> M >> T;
    vector<array<int,3>> edge(M);
    vector<int> V(T);
    for(int i = 0; i < M; i++){
        cin >> edge[i][1] >> edge[i][2] >> edge[i][0];
        edge[i][1]--, edge[i][2]--;
    }
    for(auto &&v:V)cin >> v, v--;
    if(T <= 15){
        steiner_tree<int> st(N);
        for(auto a:edge){
            st.add_edge(a[1], a[2], a[0]);
        }
        cout << st.solve(V) << endl;
    }else{
        sort(edge.begin(), edge.end());
        sort(V.begin(), V.end());
        vector<int> T;
        for(int i = 0; i < N; i++){
            if(binary_search(V.begin(), V.end(), i))continue;
            T.push_back(i);
        }
        int ans = 1 << 30;
        for(int i = 0; i < (1 << T.size()); i++){
            dsu uf(N);
            vector<bool> NotUse(N);
            for(int j = 0; j < T.size(); j++){
                if(i >> j & 1)NotUse[T[j]] = true;
            }
            int sumv = 0;
            for(int j = 0; j < M; j++){
                if(uf.same(edge[j][1], edge[j][2]) || NotUse[edge[j][1]] || NotUse[edge[j][2]])continue;
                sumv += edge[j][0];
                uf.merge(edge[j][1], edge[j][2]);
            }
            bool flag = true;
            for(int j = 1; j < V.size(); j++){
                if(!uf.same(V[0], V[j])){
                    flag = false;
                    break;
                }
            }
            if(flag)ans = min(ans, sumv);
        }
        cout << ans << '\n';
    }
}
0