結果
問題 | No.95 Alice and Graph |
ユーザー |
|
提出日時 | 2019-04-27 02:58:07 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,631 ms / 5,000 ms |
コード長 | 3,054 bytes |
コンパイル時間 | 1,871 ms |
コンパイル使用メモリ | 178,108 KB |
実行使用メモリ | 6,664 KB |
最終ジャッジ日時 | 2024-12-27 18:48:36 |
合計ジャッジ時間 | 11,240 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 14 |
ソースコード
// 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 secondusing namespace std;// typestypedef pair<int, int> P;typedef pair<ll, int> Pl;typedef pair<ll, ll> Pll;typedef pair<double, double> Pd;// constantsconst int inf = 1e9;const ll linf = 1LL << 50;const double EPS = 1e-10;const int mod = 1e9 + 7;// solvetemplate <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;}