Submission #1106140

#TimeUsernameProblemLanguageResultExecution timeMemory
1106140_callmelucianNetrpeljivost (COI23_netrpeljivost)C++14
100 / 100
331 ms33172 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

ll cost[2048][2048], dp[2][2048];

int trim (int mask, int pos) {
    return ((mask >> pos) << pos);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n; cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) cin >> cost[i][j];

    for (int i = 1; i < n; i++) {
        int t = i & 1;
        for (int last = 0; last < n; last++) {
            int cand = i & (-i), pos = 31 - __builtin_clz(cand), mask = trim(last, pos) ^ cand;
            dp[t][last] = LLONG_MAX;
            for (int preLast = mask; preLast < mask + cand; preLast++)
                dp[t][last] = min(dp[t][last], dp[t ^ 1][preLast] + cost[last][preLast]);
        }
    }

    ll ans = LLONG_MAX;
    for (int i = 0; i < n; i++) ans = min(ans, dp[(n - 1) & 1][i]);
    cout << ans;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...