#include <bits/stdc++.h>
using namespace std;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
void solve() {
int n,k;
cin >> n >> k;
int a[n+1][n+1];
F(i,n) {
F(j,n) cin >> a[i][j];
}
for (int i=1;i<=n;i++) {
for (int j=1;j<=n;j++) {
for (int m=1;m<=n;m++) {
a[i][j] = min(a[i][j],a[i][m]+a[m][j]);
}
}
}
int lim = (1<<n);
vi dp(lim,2e18);
for (int mask = 0;mask<lim;mask++) {
int pc = __builtin_popcountll(mask);
if (pc == k) {
dp[mask] = 0;
continue;
}
if (pc < k) continue;
for (int j=0;j<n;j++) {
if (mask&(1<<j)) {
for (int jj=0;jj<n;jj++) {
if (jj==j) continue;
if(!(mask&(1<<jj))) continue;
dp[mask] = min(dp[mask],dp[mask^(1<<j)]+a[j+1][jj+1]);
}
}
}
}
cout << dp[lim-1] << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int t = 1;
//cin >> t;
F(i,t) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
348 KB |
Output is correct |
6 |
Correct |
14 ms |
604 KB |
Output is correct |
7 |
Correct |
25 ms |
860 KB |
Output is correct |
8 |
Correct |
60 ms |
1368 KB |
Output is correct |
9 |
Correct |
722 ms |
8648 KB |
Output is correct |
10 |
Correct |
57 ms |
8656 KB |
Output is correct |