結果

問題 No.95 Alice and Graph
ユーザー fumiphysfumiphys
提出日時 2019-04-27 02:58:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,568 ms / 5,000 ms
コード長 3,054 bytes
コンパイル時間 1,708 ms
コンパイル使用メモリ 175,244 KB
実行使用メモリ 6,552 KB
最終ジャッジ日時 2023-08-17 10:28:37
合計ジャッジ時間 11,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,335 ms
6,540 KB
testcase_01 AC 43 ms
6,508 KB
testcase_02 AC 42 ms
6,448 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1,482 ms
6,444 KB
testcase_05 AC 1,568 ms
6,456 KB
testcase_06 AC 1,497 ms
6,504 KB
testcase_07 AC 43 ms
6,552 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 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 1,018 ms
6,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes
#include <bits/stdc++.h>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define FI first
#define SE second
using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}

template <typename T>
struct Graph_ {
  int n;
  vector<vector<T> > d;
  vector<vector<int> > path;
  Graph_(int ns) {
    n = ns;
    d = vector<vector<T> >(n, vector<T>(n, numeric_limits<T>::max() / 10));
    path = vector<vector<int> >(n, vector<int>(n, -1));
    for(int i = 0; i < n; i++)d[i][i] = 0;
  }
  void warshall_floyd(){
    for(int k = 0; k < n; k++){
      for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
          if(d[i][j] > d[i][k] + d[k][j]){
            d[i][j] = d[i][k] + d[k][j];
            path[i][j] = k;
          }
        }
      }
    }
  }
  void adde(int at, int to, T cost){
    d[at][to] = cost;
  }
};

typedef struct Graph_<int> GraphI;
typedef struct Graph_<ll> GraphL;

int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, K;
  cin >> n >> m >> K;
  GraphI graph(n);
  rep(i, m){
    int u, v;
    cin >> u >> v, u--, v--;
    graph.adde(u, v, 1);
    graph.adde(v, u, 1);
  }
  graph.warshall_floyd();
  vector<int> v;
  vector<bool> used(n, false);
  auto ok = [&](vector<int> &s){
    vector<vector<int> > dp(1 << sz(s), vector<int>(sz(s), inf));
    rep(i, sz(s))dp[1<<i][i] = graph.d[0][s[i]];
    for(int i = 0; i < (1 << sz(s)); i++){
      if(__builtin_popcount(i) < 2)continue;
      for(int j = 0; j < sz(s); j++){
        if((i >> j) % 2 == 1){
          int st = i ^ (1 << j);
          for(int k = 0; k < sz(s); k++){
            if((st >> k) % 2 == 1){
              dp[i][j] = min(dp[i][j], dp[st][k] + graph.d[s[j]][s[k]]);
            }
          }
        }
      }
    }
    rep(i, sz(s))if(dp[(1<<sz(s))-1][i] <= K)return true;
    return false;
  };
  while(v.size() < K){
    bool flag = true;
    for(int i = n - 1; i > 0; i--){
      if(!used[i]){
        v.pb(i);
        if(ok(v)){
          used[i] = true;
          break;
        }else{
          v.pop_back();
        }
      }
      if(i == 1)flag = false;
    }
    if(!flag)break;
  }
  ll res = 0;
  rep(i, sz(v))res += (1LL << v[i]) - 1;
  cout << res << endl;
	return 0;
}
0