結果

問題 No.95 Alice and Graph
ユーザー keikei
提出日時 2018-05-04 20:23:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 888 ms / 5,000 ms
コード長 2,868 bytes
コンパイル時間 1,625 ms
コンパイル使用メモリ 172,720 KB
実行使用メモリ 13,964 KB
最終ジャッジ日時 2023-09-10 09:46:50
合計ジャッジ時間 5,815 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 349 ms
13,932 KB
testcase_01 AC 51 ms
13,648 KB
testcase_02 AC 52 ms
13,740 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 528 ms
13,888 KB
testcase_05 AC 403 ms
13,884 KB
testcase_06 AC 512 ms
13,960 KB
testcase_07 AC 52 ms
13,816 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 888 ms
13,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/95>
 問題文============================================================
 1からNまでのN個のノードからなる無向グラフが存在する
 ノードkは2^(k-1)-1個のコインが存在する
 
 最初ノード1から始まり、最大でK回隣接ノードへ移動可能な時、収集可能な最大コイン数を求めよ
 ノード1へ再び戻ってくる必要な無い
 =================================================================
 解説=============================================================
 ================================================================
 */

ll check(vector<ll>& l,vector<vector<ll>>& G){
    ll res = LINF;
    ll n = l.size();
    vector<vector<ll>> dp(1<<n,vector<ll>(n,LINF));
    dp[1][0] = 0;
    for(int i = 1; i < (1<<n);i++){
        for(int j = 0; j < n; j++){
            if(dp[i][j] == LINF) continue;
            for(int k = 0; k < n;k++){
                if((i>>k)&1) continue;
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k],dp[i][j]+G[l[j]][l[k]]);
            }
        }
    }
    res = *min_element(dp[(1<<n)-1].begin(), dp[(1<<n)-1].end());
    return res;
}
ll solve(){
    ll res = 0;
    ll N,M,K; cin >> N >> M >> K;
    vector<vector<ll>> G(N,vector<ll>(N,LINF));
    for(int i = 0; i < N;i++) G[i][i] = 0;
    for(int i = 0; i < M;i++){
        ll u,v; cin >> u >> v; u--; v--;
        G[u][v] = G[v][u] = 1;
    }
    for(int i = 0; i < N;i++) for(int j = 0; j < N;j++) for(int k = 0; k < N;k++)
        G[j][k] = min(G[j][k],G[j][i]+G[i][k]);
    
    vector<ll> l; l.push_back(0);
    for(ll x = N-1; x > 0; x--){
        if(l.size() > K) break;
        l.push_back(x);
        if(check(l,G) > K){
            l.pop_back();
        }
    }
    for(auto v:l){ res += (1LL<<v) - 1; }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}
0