結果

問題 No.95 Alice and Graph
ユーザー koba-e964
提出日時 2016-12-29 23:59:35
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 864 ms / 5,000 ms
コード長 2,287 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 95,532 KB
実行使用メモリ 7,636 KB
最終ジャッジ日時 2024-12-15 17:00:13
合計ジャッジ時間 4,455 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0