답안 #642529

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
642529 2022-09-19T18:21:44 Z Urvuk3 Kronican (COCI16_kronican) C++17
100 / 100
724 ms 8532 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int MAXN=20,MAXA=5e6+5,INF=1e9;
#define fi first
#define se second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define mid (l+r)/2
#define sz(a) int((a).size())
#define all(a) a.begin(),a.end()
#define mod 1000000007LL
#define pb push_back
#define endl "\n"
#define PRINT(x) cerr<<#x<<'-'<<x<<endl<<flush;
#define PRINTBITS(x) {bitset<5> f=x; cerr<<#x<<'-'<<f<<endl<<flush;}
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pb push_back
#define pf push_front
#define ppf pop_front
#define ppb pop_back

ll n,m,k,q,x,y,z,res=INF,l,r;
string s,t;
vector<int> a;
ifstream in;

#ifdef ONLINE_JUDGE
    #define in cin
#endif

vector<ll> dp((1<<MAXN),INF);
int c[MAXN][MAXN];

void solve(){
    cin>>n>>k;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>c[i][j];
        }
    }
    dp[(1<<n)-1]=0; // sve su pune
    for(int m1=(1<<n)-1;m1>=0;m1--){ // 1 ako je casa puna
        for(int i=0;i<n;i++){ // i-ta casa je puna m1&(1<<i)
            for(int j=0;j<n;j++){ // j-ta casa je puna m1&(1<<j)
                if(m1&(1<<i) && m1&(1<<j) && i!=j){
                    ll m2=m1^(1<<i); // i-ta postaje prazna
                    dp[m2]=min(dp[m2],dp[m1]+c[i][j]);
                }
            }
        }
    }
    for(int m1=(1<<n)-1;m1>=0;m1--){
        if(__builtin_popcount(m1)<=k){
            res=min(res,dp[m1]);
        }
    }
    cout<<res<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        in.open("D:\\UROS\\Programiranje\\input.in",ios::in);
    #endif
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int t;
    //cin>>t;
    t=1;
    while(t--){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8532 KB Output is correct
2 Correct 4 ms 8532 KB Output is correct
3 Correct 4 ms 8532 KB Output is correct
4 Correct 4 ms 8532 KB Output is correct
5 Correct 11 ms 8532 KB Output is correct
6 Correct 20 ms 8532 KB Output is correct
7 Correct 40 ms 8532 KB Output is correct
8 Correct 79 ms 8532 KB Output is correct
9 Correct 722 ms 8532 KB Output is correct
10 Correct 724 ms 8532 KB Output is correct