結果

問題 No.95 Alice and Graph
ユーザー koba-e964koba-e964
提出日時 2016-12-29 23:59:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 803 ms / 5,000 ms
コード長 2,287 bytes
コンパイル時間 1,384 ms
コンパイル使用メモリ 95,076 KB
実行使用メモリ 7,676 KB
最終ジャッジ日時 2023-08-22 00:30:36
合計ジャッジ時間 4,786 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 232 ms
7,436 KB
testcase_01 AC 36 ms
7,660 KB
testcase_02 AC 37 ms
7,568 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 361 ms
7,388 KB
testcase_05 AC 280 ms
7,676 KB
testcase_06 AC 349 ms
7,472 KB
testcase_07 AC 37 ms
7,372 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 803 ms
7,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;

const int inf = 1e7;

const int K = 16;
int dp[1 << K][K];

// Checks if there exists a Hamiltonian path with length <= k.
bool check(const vector<VI> &dist, const VI &route, int k) {
  assert (route[0] == 0);
  int m = route.size();
  assert (m <= k + 1);
  REP(i, 0, 1 << m) {
    REP(j, 0, m) {
      dp[i][j] = inf;
    }
  }
  dp[1][0] = 0;
  for (int bits = 3; bits < 1 << m; bits += 2) {
    REP(i, 0, m) {
      if ((bits & 1 << i) == 0) { continue; }
      REP(j, 0, m) {
	if (i == j || (bits & 1 << j) == 0) { continue; }
	dp[bits][i] = min(dp[bits][i], dp[bits ^ 1 << i][j]
			  + dist[route[i]][route[j]]);
      }
    }
  }
  int mi = inf;
  REP(i, 0, m) {
    mi = min(mi, dp[(1 << m) - 1][i]);
  }
  if (0) {
    cerr << "solve(";
    REP(i, 0, m) {
      cerr << route[i] << (i == m - 1 ? "" : " ");
    }
    cerr << ") = " << mi << endl;
  }
  return mi <= k;
}


// I solved this problem after reading the editorial
int main(void){
  int n, m, k;
  cin >> n >> m >> k;
  vector<VI> edges(n);
  vector<VI> dist(n, VI(n, inf));
  REP(i, 0, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    edges[x].push_back(y);
    edges[y].push_back(x);
    dist[x][y] = 1;
    dist[y][x] = 1;
  }
  REP(i, 0, n) {
    dist[i][i] = 0;
  }
  REP(l, 0, n) {
    REP(i, 0, n) {
      REP(j, 0, n) {
	dist[i][j] = min(dist[i][j], dist[i][l] + dist[l][j]);
      }
    }
  }
  VI route(1, 0);
  ll tot = 0;
  for (int i = n - 1; i >= 1; --i) {
    if (route.size() >= k + 1) {
      break;
    }
    route.push_back(i);
    if (check(dist, route, k)) {
      tot += (1LL << i) - 1;
    } else {
      route.pop_back();
    }
  }
  assert (route.size() <= k + 1);
  cout << tot << endl;
}
0