답안 #317321

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
317321 2020-10-29T13:27:56 Z fadi57 Kronican (COCI16_kronican) C++14
100 / 100
803 ms 8576 KB
#include <bits/stdc++.h>
using namespace std;
const long long mx=21;
const long long mod=1e9+7;
typedef long long ll;
int a[mx][mx];
int dp[1<<mx];int k;int n,m;
int solve(int mask){
     if (__builtin_popcount(mask) == k) return 0;
     int &ret=dp[mask];
    
     if(ret!=-1){return ret;}
     ret=1<<30;
     for(int i=0;i<n;i++){
         if(!(mask&(1<<i))){continue;}
         for(int j=0;j<n;j++){
             if(i==j){continue;}
              if(!(mask&(1<<j))){continue;}
           
                  ret=min(ret,solve(mask^(1<<i))+a[i][j]);
         }
     }
    return ret;
    
    
}



int main() { 
memset(dp,-1,sizeof(dp));
cin>>n>>k;
for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
    cin>>a[i][j];//cout<<a[i][j];
        
    }
}
int all=(1<<n)-1;int ans=1<<30;
dp[all]=0;
for(int mask=all;mask>=1;mask--){
    if(__builtin_popcount(mask)==k){ans=min(ans,dp[mask]);}
    for(int i=0;i<n;i++){
        if(!((1<<i)&mask)){
            continue;
        }
        for(int j=0;j<n;j++){
             if(!((1<<j)&mask)){
            continue;
                 }
        if(i==j){continue;}
        int mask2=mask^(1<<i);
       // dp[mask2]=min(dp[mask2],dp[mask]+a[i][j]);
         // f[nmsk] = (f[nmsk] == -1 ? f[msk] + a[i][j] : min(f[nmsk], f[msk] + a[i][j]));
          dp[mask2]=(dp[mask2]==-1?dp[mask]+a[i][j]:min(dp[mask2],dp[mask]+a[i][j]));
        }
    }
    
}
cout<<ans;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8576 KB Output is correct
2 Correct 5 ms 8576 KB Output is correct
3 Correct 5 ms 8576 KB Output is correct
4 Correct 5 ms 8576 KB Output is correct
5 Correct 13 ms 8576 KB Output is correct
6 Correct 23 ms 8576 KB Output is correct
7 Correct 42 ms 8572 KB Output is correct
8 Correct 83 ms 8568 KB Output is correct
9 Correct 792 ms 8568 KB Output is correct
10 Correct 803 ms 8568 KB Output is correct