QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#628210#7185. Poor Studentsucup-team3519#TL 2668ms9060kbC++172.7kb2024-10-10 19:08:522024-10-10 19:08:54

Judging History

你现在查看的是最新测评结果

  • [2024-10-10 19:08:54]
  • 评测
  • 测评结果:TL
  • 用时:2668ms
  • 内存:9060kb
  • [2024-10-10 19:08:52]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = int64_t;
constexpr i64 INF = 1e18;

template <typename T>
class ZKW {
    struct Edge {
        int to, cap;
        T cost;
    };

    std::vector<Edge> edge;
    std::vector<std::vector<int>> adj;
    std::vector<T> d;
    std::vector<uint8_t> vist;

    int s, t;
    int flow;
    T cost;

    std::vector<int> ns;

    int dfs(int x, int f) {
        vist[x] = true;
        ns.push_back(x);
        if (x == t) {
            flow += f;
            cost += -d[s] * f;
            return f;
        }
        for (int i : adj[x]) {
            int y = edge[i].to;
            if (edge[i].cap && !vist[y] && d[x] + edge[i].cost - d[y] == 0) {
                int res = dfs(y, std::min(f, edge[i].cap));
                if (res) {
                    edge[i].cap -= res;
                    edge[i ^ 1].cap += res;
                    return res;
                }
            }
        }
        return 0;
    }
    bool relabel() {
        if (vist[t]) {
            return true;
        }
        T z = INF;
        for (int x : ns) {
            for (int i : adj[x]) {
                int y = edge[i].to;
                if (edge[i].cap && !vist[y]) {
                    z = std::min(z, d[x] + edge[i].cost - d[y]);
                }
            }
        }
        if (z == INF) {
            return false;
        }
        for (int i : ns) {
            d[i] -= z;
        }
        return true;
    }

public:
    explicit ZKW(int n) : adj(n), d(n), vist(n) {}
    void add_edge(int u, int v, int cap, T cost) {
        adj[u].push_back(edge.size());
        edge.push_back(Edge{v, cap, cost});
        adj[v].push_back(edge.size());
        edge.push_back(Edge{u, 0, -cost});
    }
    std::pair<int, T> operator()(int s, int t) {
        this->s = s;
        this->t = t;
        flow = 0;
        cost = 0;
        do {
            for (int i : ns) {
                vist[i] = false;
            }
            ns.clear();
            dfs(s, 1e9);
        } while (relabel());
        return std::make_pair(flow, cost);
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, k;
    std::cin >> n >> k;

    ZKW<i64> f(n + k + 2);
    const int s = n + k + 1, t = n + k;

    for (int i = 0; i < n; ++i) {
        f.add_edge(s, i, 1, 0);
        for (int j = n; j < n + k; ++j) {
            int c;
            std::cin >> c;
            f.add_edge(i, j, 1, c);
        }
    }
    for (int i = n; i < n + k; ++i) {
        int a;
        std::cin >> a;
        f.add_edge(i, t, a, 0);
    }

    auto [flow, cost] = f(s, t);
    assert(flow == n);
    std::cout << cost << '\n';

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3608kb

input:

6 2
1 2
1 3
1 4
1 5
1 6
1 7
3 4

output:

12

result:

ok answer is '12'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

3 3
1 2 3
2 4 6
6 5 4
1 1 1

output:

8

result:

ok answer is '8'

Test #3:

score: 0
Accepted
time: 21ms
memory: 3892kb

input:

1000 10
734 303 991 681 755 155 300 483 702 442
237 256 299 675 671 757 112 853 759 233
979 340 288 377 718 199 935 666 576 842
537 363 592 349 494 961 864 727 84 813
340 78 600 492 118 421 478 925 552 617
517 589 716 7 928 638 258 297 706 787
266 746 913 978 436 859 701 951 137 44
815 336 471 720 2...

output:

92039

result:

ok answer is '92039'

Test #4:

score: 0
Accepted
time: 566ms
memory: 5764kb

input:

5000 10
14 114 254 832 38 904 25 147 998 785
917 694 750 372 379 887 247 817 999 117
802 15 799 515 316 42 69 247 95 144
727 398 509 725 682 456 369 656 693 955
923 1 681 631 962 826 233 963 289 856
165 491 488 832 111 950 853 791 929 240
509 843 667 970 469 260 447 477 161 431
514 903 627 236 144 3...

output:

461878

result:

ok answer is '461878'

Test #5:

score: 0
Accepted
time: 2668ms
memory: 9060kb

input:

10000 10
307 205 765 487 504 526 10 581 234 583
448 443 39 992 976 363 335 588 588 169
920 787 896 822 47 358 230 631 136 299
141 159 414 852 922 945 513 76 111 189
616 104 83 792 24 68 164 975 615 472
150 108 848 517 7 153 107 283 452 165
94 370 910 662 226 720 975 214 324 407
636 65 963 859 590 3 ...

output:

919745

result:

ok answer is '919745'

Test #6:

score: -100
Time Limit Exceeded

input:

50000 10
819 49 278 985 747 872 146 129 898 569
929 427 54 846 136 475 448 304 591 428
238 844 664 991 990 863 308 571 867 958
775 690 792 697 557 325 824 654 303 833
542 942 262 534 501 575 273 60 701 488
733 855 810 405 294 909 638 975 801 836
382 265 818 765 240 69 980 889 472 211
629 434 128 389...

output:


result: