結果

問題 No.3158 Collect Stamps
コンテスト
ユーザー it_is_a_pity_
提出日時 2025-05-23 20:33:51
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,744 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,233 ms
コンパイル使用メモリ 230,292 KB
最終ジャッジ日時 2026-07-11 00:17:06
合計ジャッジ時間 2,077 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_algobase.h:76,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/algorithm:62,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:53,
                 from main.cpp:4:
/home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bit: In instantiation of 'constexpr int std::__popcount(_Tp) [with _Tp = long long int]':
main.cpp:47:22:   required from here
   47 |         if(__popcount(i)!=M){
      |            ~~~~~~~~~~^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bit:308:34: error: argument 1 in call to function '__builtin_popcountg' has signed type
  308 |       return __builtin_popcountg(__x);
      |                                  ^~~

ソースコード

diff #
raw source code

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define all(v) v.begin(),v.end()
const ll INF = (ll)2e18;

void warshall_floyd(vector<vector<long long>> &dist) {
    int V = dist.size();
    for (int k = 0; k < V; k++) {
        for (int i = 0; i < V; i++) {
            for (int j = 0; j < V; j++) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, M, K;
    cin >> N >> M >> K;
    vector<ll> A(K);
    set<ll> destinations;

    rep(i, K){
        cin >> A[i];
        A[i]--;
        destinations.insert(A[i]);
    }
    vector<vector<ll>> T(N, vector<ll>(N));
    rep(i,N){
        rep(j,N){
            cin >> T[i][j];
        }
    }
    warshall_floyd(T);
    ll ans = INF;
    rep(i,(1LL<<N)){
        if(__popcount(i)!=M){
            continue;
        }
        vector<ll> pos;
        rep(j,N){
            if((i>>j)&1){
                pos.push_back(j);
            }
        }
        vector<ll> order(M);
        rep(j,M){
            order[j] = j;
        }
        do{
            ll sum = 0;
            rep(j,M-1){
                sum += T[pos[order[j]]][pos[order[j + 1]]];
            }
            if(!destinations.count(pos[order.back()])){
                ll MIN = INF;
                rep(j,K){
                    MIN = min(MIN, T[pos[order.back()]][A[j]]);
                }
                sum += MIN;
            }
            ans = min(ans, sum);
        } while (next_permutation(all(order)));
    }
    cout << ans << endl;
}
0