Submission #1106138

#TimeUsernameProblemLanguageResultExecution timeMemory
1106138_callmelucianNetrpeljivost (COI23_netrpeljivost)C++14
100 / 100
569 ms107124 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[2048][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++) {
        for (int last = 0; last < n; last++) {
            int cand = i & (-i), pos = 31 - __builtin_clz(cand), mask = trim(last, pos) ^ cand;
            dp[i][last] = LLONG_MAX;
            for (int preLast = mask; preLast < mask + cand; preLast++)
                dp[i][last] = min(dp[i][last], dp[i - 1][preLast] + cost[preLast][last]);
        }
    }

    ll ans = LLONG_MAX;
    for (int i = 0; i < n; i++) ans = min(ans, dp[n - 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...