結果

問題 No.114 遠い未来
ユーザー t98slidert98slider
提出日時 2022-06-17 02:14:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,340 bytes
コンパイル時間 4,163 ms
コンパイル使用メモリ 161,028 KB
実行使用メモリ 165,412 KB
最終ジャッジ日時 2023-07-29 09:24:12
合計ジャッジ時間 16,288 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
4,380 KB
testcase_01 AC 2,157 ms
4,384 KB
testcase_02 AC 271 ms
4,380 KB
testcase_03 AC 56 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 7 ms
4,380 KB
testcase_06 AC 799 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 11 ms
4,380 KB
testcase_10 AC 163 ms
4,608 KB
testcase_11 AC 465 ms
5,936 KB
testcase_12 AC 1,499 ms
4,380 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<atcoder/all>
using namespace std;

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 <= 14){
        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++){
            atcoder::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