結果

問題 No.3158 Collect Stamps
ユーザー it_is_a_pity_
提出日時 2025-05-23 20:33:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,744 bytes
コンパイル時間 4,331 ms
コンパイル使用メモリ 259,744 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-23 20:34:13
合計ジャッジ時間 5,737 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#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