結果

問題 No.114 遠い未来
ユーザー t98slidert98slider
提出日時 2022-06-17 02:23:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,324 bytes
コンパイル時間 838 ms
コンパイル使用メモリ 94,788 KB
最終ジャッジ日時 2024-04-16 16:18:47
合計ジャッジ時間 1,343 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:55:5: error: 'cin' was not declared in this scope
   55 |     cin >> N >> M >> T;
      |     ^~~
main.cpp:2:1: note: 'std::cin' is defined in header '<iostream>'; did you forget to '#include <iostream>'?
    1 | #include<atcoder/all>
  +++ |+#include <iostream>
    2 | using namespace std;
main.cpp:68:9: error: 'cout' was not declared in this scope
   68 |         cout << st.solve(V) << endl;
      |         ^~~~
main.cpp:68:9: note: 'std::cout' is defined in header '<iostream>'; did you forget to '#include <iostream>'?
main.cpp:68:32: error: 'endl' was not declared in this scope
   68 |         cout << st.solve(V) << endl;
      |                                ^~~~
main.cpp:2:1: note: 'std::endl' is defined in header '<ostream>'; did you forget to '#include <ostream>'?
    1 | #include<atcoder/all>
  +++ |+#include <ostream>
    2 | using namespace std;
main.cpp:99:9: error: 'cout' was not declared in this scope
   99 |         cout << ans << '\n';
      |         ^~~~
main.cpp:99:9: note: 'std::cout' is defined in header '<iostream>'; did you forget to '#include <iostream>'?

ソースコード

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.emplace(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.emplace(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++){
            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