답안 #93181

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
93181 2019-01-07T03:36:16 Z lalakep Kronican (COCI16_kronican) C++14
80 / 100
514 ms 33792 KB
#include <bits/stdc++.h>

using namespace std;
const int Nmax=27;
const int oo=1e9+7;
int n,k;
int f[Nmax][Nmax];
int dp[(1<<20)+7];
int best[Nmax][(1<<20)+7];
int slv(int bitmask)
{
    int res=__builtin_popcount(bitmask);
    if (res==k) return 0;
    if (dp[bitmask]!=-1) return dp[bitmask];
    int cur=1e9;
    for (int i=0;i<=n-1;i++)
        if ((bitmask&(1<<i))!=0) cur=min(cur,slv(bitmask^(1<<i))+f[i+1][best[i+1][bitmask]]);
    return dp[bitmask]=cur;
}
void build_best()
{
    for (int bitmask=0;bitmask<=(1<<n);bitmask++)
    {
        int res=__builtin_popcount(bitmask);
        if (res>=k)
        {
            for (int i=0;i<=n-1;i++)
            {
                if ((bitmask&(1<<i))!=0)
                {
                    best[i+1][bitmask]=0;
                    for (int j=0;j<=n-1;j++)
                    {
                        if ((bitmask&(1<<j))!=0&&i!=j)
                            if (f[i+1][j+1]<f[i+1][best[i+1][bitmask]]) best[i+1][bitmask]=j+1;
                    }
                }
            }
        }
    }
}
//void trace(int bitmask)
//{
//    int res=__builtin_popcount(bitmask);
//    if (res==k)
//    {
//        cout<<bitmask<<"\n";
//        return;
//    }
//    for (int i=0;i<=n-1;i++)
//        if ((bitmask&(1<<i))!=0)
//        {
//            if (slv(bitmask)==slv(bitmask^(1<<i))+f[i+1][best[i+1][bitmask]]);
//            {
//                trace(bitmask^(1<<i));
//                //break;
//            }
//        }
//}
int main()
{
    //freopen("kronican.inp","r",stdin);
    //freopen("kronican.out","w",stdout);
    cin>>n>>k;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++) cin>>f[i][j];
    for (int i=1;i<=n;i++) f[i][0]=oo;
    build_best();
    memset(dp,-1,sizeof dp);
    cout<<slv((1<<n)-1)<<"\n";
    //trace((1<<n)-1);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4444 KB Output is correct
2 Correct 4 ms 4472 KB Output is correct
3 Correct 5 ms 4600 KB Output is correct
4 Correct 5 ms 4472 KB Output is correct
5 Correct 14 ms 5368 KB Output is correct
6 Correct 28 ms 6264 KB Output is correct
7 Correct 55 ms 8184 KB Output is correct
8 Correct 121 ms 12060 KB Output is correct
9 Runtime error 514 ms 33792 KB Execution killed with signal 9 (could be triggered by violating memory limits)
10 Runtime error 88 ms 33792 KB Execution killed with signal 9 (could be triggered by violating memory limits)