#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <numeric>
#include <cassert>
#define endl '\n'
#define sp ' '
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn = 25;
const int inf = 1e9;
int c[maxn][maxn];
int dp[1<<20];
int popcount(int n){
int ret = 0;
while(n){
if(n&1)
ret++;
n>>=1;
}
return ret;
}
int n,k;
int f(int mask){
if(popcount(mask) == k)
return 0;
if(dp[mask] != -1)
return dp[mask];
int ret = inf;
for(int i=0;i<n;i++){
if(mask & (1<<i)){
for(int j=0;j<n;j++){
if(i==j)
continue;
if(mask & (1<<j)){
ret = min(ret,f(mask ^ (1<<i)) + c[i][j]);
}
}
}
}
return dp[mask] = ret;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cerr.tie(nullptr);
memset(dp,-1,sizeof dp);
cin>>n>>k;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>c[i][j];
cout << f((1<<n)-1) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4460 KB |
Output is correct |
2 |
Correct |
3 ms |
4460 KB |
Output is correct |
3 |
Correct |
3 ms |
4460 KB |
Output is correct |
4 |
Correct |
4 ms |
4460 KB |
Output is correct |
5 |
Correct |
18 ms |
4588 KB |
Output is correct |
6 |
Correct |
42 ms |
4608 KB |
Output is correct |
7 |
Correct |
90 ms |
4460 KB |
Output is correct |
8 |
Correct |
216 ms |
4588 KB |
Output is correct |
9 |
Execution timed out |
2052 ms |
4460 KB |
Time limit exceeded |
10 |
Correct |
285 ms |
4588 KB |
Output is correct |