QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183350#7185. Poor StudentsScarlett_boyTL 137ms12756kbC++172.3kb2023-09-19 14:00:212023-09-19 14:00:21

Judging History

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

  • [2023-09-19 14:00:21]
  • 评测
  • 测评结果:TL
  • 用时:137ms
  • 内存:12756kb
  • [2023-09-19 14:00:21]
  • 提交

answer

#include "bits/stdc++.h"

typedef long long ll;
using namespace std;

const int N = 5e4 + 10, M = 5e5 + 10;
#define int long long

struct SSP {
    int cnt = 1, hd[N], nxt[M << 1], to[M << 1], limit[M << 1], cst[M << 1];

    void add(int u, int v, int w, int c) {
        nxt[++cnt] = hd[u], hd[u] = cnt, to[cnt] = v, limit[cnt] = w, cst[cnt] = c;
        nxt[++cnt] = hd[v], hd[v] = cnt, to[cnt] = u, limit[cnt] = 0, cst[cnt] = -c;
    }

    int fr[N], fl[N], in[N], dis[N];

    pair<int, int> min_cost(int s, int t) {
        int flow = 0, cost = 0;
        while (true) { // SPFA
            queue<int> q;
            memset(dis, 0x3f, sizeof(dis));
            memset(in, 0, sizeof(in));
            fl[s] = 1e18, dis[s] = 0, q.push(s);
            while (!q.empty()) {
                int cur = q.front();
                q.pop(), in[cur] = 0;
                for (int i = hd[cur]; i; i = nxt[i]) {
                    int it = to[i], d = dis[cur] + cst[i];
                    if (limit[i] && d < dis[it]) {
                        fl[it] = min(limit[i], fl[cur]), fr[it] = i, dis[it] = d;
                        if (!in[it]) in[it] = 1, q.push(it);
                    }
                }
            }
            if (dis[t] > 1e18) return {flow, cost};
            flow += fl[t], cost += dis[t] * fl[t];
            for (int u = t; u != s; u = to[fr[u] ^ 1]) limit[fr[u]] -= fl[t], limit[fr[u] ^ 1] += fl[t];
        }
    }
} G;

int n, k;
int S, T;

void solve() {
    // int u, v, w, c;
    // G.add(u, v, w, c);
    // u - > v  流量为1 ,费用为 c
    cin >> n >> k;
    S = 0, T = n + k + 1;
    for (int i = 1; i <= n; i++) {
        G.add(S, i, 1, 0);
    }
    for (int i = 1; i <= n; i++) {
        int c;
        for (int j = 1; j <= k; j++) {
            cin >> c;
            G.add(i, n + j, 1, c);
        }
    }
    for (int i = 1; i <= k; i++) {
        int a;
        cin >> a;
        G.add(n + i, T, a, 0);
    }
    auto [flow, cost] = G.min_cost(S, T);
    cout << cost << '\n';
}


signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _ = 1;
//    cin >> _;
    for (int o = 1; o <= _; o++) {
        solve();
    }
    return 0;
}
/*


33333 99999
133331
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 12748kb

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: 137ms
memory: 12756kb

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: -100
Time Limit Exceeded

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:


result: