Submission #66129528


Source Code Expand

#include <bits/stdc++.h>
#define ll long long

using namespace std;

typedef vector<ll> VD;
typedef vector<VD> VVD;
typedef vector<int> VI;

ll MinCostMatching(const VVD &cost, VI &Lmate, VI &Rmate) {
    int n = int(cost.size());

    // construct dual feasible solution
    VD u(n);
    VD v(n);
    for (int i = 0; i < n; i++) {
        u[i] = cost[i][0];
        for (int j = 1; j < n; j++) u[i] = min(u[i], cost[i][j]);
    }
    for (int j = 0; j < n; j++) {
        v[j] = cost[0][j] - u[0];
        for (int i = 1; i < n; i++) v[j] = min(v[j], cost[i][j] - u[i]);
    }

    // construct primal solution satisfying complementary slackness
    Lmate = VI(n, -1);
    Rmate = VI(n, -1);
    int mated = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (Rmate[j] != -1) continue;
            if (fabs(cost[i][j] - u[i] - v[j]) < 1e-10) {
                Lmate[i] = j;
                Rmate[j] = i;
                mated++;
                break;
            }
        }
    }

    VD dist(n);
    VI dad(n);
    VI seen(n);

    // repeat until primal solution is feasible
    while (mated < n) {
        // find an unmatched left node
        int s = 0;
        while (Lmate[s] != -1) s++;

        // initialize Dijkstra
        fill(dad.begin(), dad.end(), -1);
        fill(seen.begin(), seen.end(), 0);
        for (int k = 0; k < n; k++) dist[k] = cost[s][k] - u[s] - v[k];

        int j = 0;
        while (true) {
            // find closest
            j = -1;
            for (int k = 0; k < n; k++) {
                if (seen[k]) continue;
                if (j == -1 || dist[k] < dist[j]) j = k;
            }
            seen[j] = 1;

            // termination condition
            if (Rmate[j] == -1) break;

            // relax neighbors
            const int i = Rmate[j];
            for (int k = 0; k < n; k++) {
                if (seen[k]) continue;
                const ll new_dist = dist[j] + cost[i][k] - u[i] - v[k];
                if (dist[k] > new_dist) {
                    dist[k] = new_dist;
                    dad[k] = j;
                }
            }
        }

        // update dual variables
        for (int k = 0; k < n; k++) {
            if (k == j || !seen[k]) continue;
            const int i = Rmate[k];
            v[k] += dist[k] - dist[j];
            u[i] -= dist[k] - dist[j];
        }
        u[s] += dist[j];

        // augment along path
        while (dad[j] >= 0) {
            const int d = dad[j];
            Rmate[j] = Rmate[d];
            Lmate[Rmate[j]] = j;
            j = d;
        }
        Rmate[j] = s;
        Lmate[s] = j;

        mated++;
    }

    ll value = 0;
    for (int i = 0; i < n; i++) value += cost[i][Lmate[i]];

    return value;
}

const int dx[4] = { 0, 0, -1, 1 };
const int dy[4] = { -1, 1, 0, 0 };
int N, M;

inline int from(int x, int y) {
    return x * M + y;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> N >> M;
    VVD A(N, VD(M, 0));
    ll sum = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> A[i][j];
            sum += A[i][j];
        }
    }
    const int ALL = N * M;
    VVD cost(ALL, VD(ALL, 0));
    VI Lmate(ALL), Rmate(ALL);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            for (int dir = 0; dir < 4; dir++) {
                int nx = i + dx[dir];
                int ny = j + dy[dir];
                if (nx >= 0 && ny >= 0 && nx < N && ny < M) {
                    ll C = A[i][j] + A[nx][ny];
                    if (C < 0) {
                        cost[from(i, j)][from(nx, ny)] = C;
                        cost[from(nx, ny)][from(i, j)] = C;
                        Lmate[from(i, j)] = from(nx, ny);
                        Rmate[from(i, j)] = from(nx, ny);
                        Lmate[from(nx, ny)] = from(i, j);
                        Rmate[from(nx, ny)] = from(i, j);
                    }
                }
            }
        }
    }
    auto matching = MinCostMatching(cost, Lmate, Rmate);
    cout << sum - matching / 2 << endl;
}

Submission Info

Submission Time
Task G - Domino Covering SUM
User Mitscho
Language C++ 17 (gcc 12.2)
Score 0
Code Size 4263 Byte
Status TLE
Exec Time 2212 ms
Memory 34624 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 44
TLE × 30
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 01_random_64.txt, 01_random_65.txt, 01_random_66.txt, 01_random_67.txt, 01_random_68.txt, 01_random_69.txt, 01_random_70.txt, 01_random_71.txt, 01_random_72.txt, 01_random_73.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3492 KiB
00_sample_01.txt AC 1 ms 3312 KiB
00_sample_02.txt AC 1 ms 3468 KiB
01_random_03.txt TLE 2209 ms 33128 KiB
01_random_04.txt AC 6 ms 3568 KiB
01_random_05.txt AC 19 ms 4364 KiB
01_random_06.txt AC 1198 ms 17924 KiB
01_random_07.txt TLE 2209 ms 29744 KiB
01_random_08.txt TLE 2209 ms 31656 KiB
01_random_09.txt TLE 2209 ms 32312 KiB
01_random_10.txt TLE 2212 ms 31952 KiB
01_random_11.txt TLE 2149 ms 32548 KiB
01_random_12.txt TLE 2209 ms 33596 KiB
01_random_13.txt TLE 2209 ms 34596 KiB
01_random_14.txt TLE 2209 ms 34076 KiB
01_random_15.txt AC 33 ms 34148 KiB
01_random_16.txt AC 31 ms 33976 KiB
01_random_17.txt AC 30 ms 31656 KiB
01_random_18.txt AC 32 ms 33196 KiB
01_random_19.txt AC 236 ms 33672 KiB
01_random_20.txt AC 1212 ms 34028 KiB
01_random_21.txt AC 1375 ms 33084 KiB
01_random_22.txt AC 1651 ms 33944 KiB
01_random_23.txt AC 895 ms 33688 KiB
01_random_24.txt AC 193 ms 34204 KiB
01_random_25.txt AC 228 ms 32904 KiB
01_random_26.txt AC 581 ms 31848 KiB
01_random_27.txt AC 735 ms 31916 KiB
01_random_28.txt AC 448 ms 32324 KiB
01_random_29.txt AC 431 ms 34016 KiB
01_random_30.txt AC 553 ms 33748 KiB
01_random_31.txt TLE 2209 ms 32916 KiB
01_random_32.txt AC 1 ms 3484 KiB
01_random_33.txt AC 1 ms 3420 KiB
01_random_34.txt AC 1 ms 3444 KiB
01_random_35.txt AC 1 ms 3328 KiB
01_random_36.txt AC 1 ms 3524 KiB
01_random_37.txt AC 1 ms 3608 KiB
01_random_38.txt AC 1 ms 3480 KiB
01_random_39.txt AC 1 ms 3524 KiB
01_random_40.txt AC 21 ms 23108 KiB
01_random_41.txt AC 22 ms 23136 KiB
01_random_42.txt TLE 2209 ms 32680 KiB
01_random_43.txt TLE 2209 ms 32568 KiB
01_random_44.txt TLE 2209 ms 34092 KiB
01_random_45.txt TLE 2209 ms 33916 KiB
01_random_46.txt TLE 2209 ms 32296 KiB
01_random_47.txt TLE 2209 ms 34220 KiB
01_random_48.txt TLE 2127 ms 32000 KiB
01_random_49.txt TLE 2209 ms 33596 KiB
01_random_50.txt TLE 2209 ms 31748 KiB
01_random_51.txt TLE 2209 ms 33120 KiB
01_random_52.txt TLE 2209 ms 31756 KiB
01_random_53.txt TLE 2209 ms 33260 KiB
01_random_54.txt TLE 2209 ms 32964 KiB
01_random_55.txt TLE 2209 ms 33612 KiB
01_random_56.txt TLE 2209 ms 32552 KiB
01_random_57.txt TLE 2209 ms 32000 KiB
01_random_58.txt TLE 2209 ms 33680 KiB
01_random_59.txt TLE 2209 ms 33628 KiB
01_random_60.txt TLE 2209 ms 32460 KiB
01_random_61.txt TLE 2209 ms 33024 KiB
01_random_62.txt AC 1 ms 3484 KiB
01_random_63.txt AC 1 ms 3448 KiB
01_random_64.txt AC 36 ms 31908 KiB
01_random_65.txt AC 41 ms 33680 KiB
01_random_66.txt AC 37 ms 31500 KiB
01_random_67.txt AC 40 ms 32872 KiB
01_random_68.txt AC 37 ms 33472 KiB
01_random_69.txt AC 40 ms 33096 KiB
01_random_70.txt AC 43 ms 34548 KiB
01_random_71.txt AC 33 ms 34624 KiB
01_random_72.txt AC 33 ms 34476 KiB
01_random_73.txt AC 32 ms 31580 KiB