QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#171570#7185. Poor Studentsucup-team870#TL 3006ms6816kbC++173.3kb2023-09-09 17:14:202023-09-09 17:14:22

Judging History

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

  • [2023-09-09 17:14:22]
  • 评测
  • 测评结果:TL
  • 用时:3006ms
  • 内存:6816kb
  • [2023-09-09 17:14:20]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
using namespace std;

struct node {
    int to, next, flow, cost;
} ed[2000010];

typedef int ll;
typedef pair<ll, int> P;

int s, t, head[100010], sz = -1, vis[100010];
ll spend, dis[100010], h[100010];

void addEdge(int from, int to, int flow, int cost) {
    sz++;
    ed[sz].to = to;
    ed[sz].next = head[from];
    ed[sz].flow = flow;
    ed[sz].cost = cost;
    head[from] = sz;
}

void add(int from, int to, int flow, int cost) {
    addEdge(from, to, flow, cost);
    addEdge(to, from, 0, -cost);
}

bool dij(int s, int t) {
    memset(vis, 0, sizeof(vis));
    for (int i =  0; i <=t ;i++) dis[i] = 1e9;
    priority_queue<P, vector<P>, greater<P> > q;
    q.push(P(0, t));
    dis[t] = 0;
    // cout <<s;
    while (!q.empty()) {
        int u = q.top().second;
        q.pop();
        if(vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u]; i!=-1; i = ed[i].next) {
            int v = ed[i].to;
            if (ed[i ^ 1].flow && dis[v] > dis[u] + (h[u] - h[v] + ed[i^1].cost)) {
                dis[v] = dis[u] + (h[u] - h[v] + ed[i^1].cost);
                q.push(P(dis[v], v));
                // cout << dis[v];
            }
        }
    }
    return dis[s] != ll(1e9);
}

// bool spfa(int s, int t) {
//     memset(vis, 0, sizeof(vis));
//     for (int i = 0;i <= t;i++) dis[i] = 1e18;
//     dis[t] = 0;
//     vis[t] = 1;
//     queue <int> q;
//     q.push(t);
//     while (!q.empty()) {
//         int u = q.front();
//         q.pop();
//         vis[u] = 0;
//         for (int i = head[u];i != -1;i = ed[i].next) {
//             int v = ed[i].to;
//             if (ed[i ^ 1].flow && dis[v] > dis[u] - ed[i].cost) {
//                 dis[v] = dis[u] - ed[i].cost;
//                 if (!vis[v]) {
//                     vis[v] = 1;
//                     q.push(v);
//                 }
//             }
//         }
//     }
//     return dis[s] != 1e9;
// }

int dfs(int u, int low) {
    if (u == t) {
        vis[t] = 1;
        return low;
    }
    int used = 0;
    vis[u] = 1;
    for (int i = head[u];~i;i = ed[i].next) {
        int v = ed[i].to;
        if (!vis[v] && ed[i].flow > 0 && dis[u] - dis[v] == ed[i].cost+h[v]-h[u]) {
            int now = dfs(v, min(low - used, ed[i].flow));
            if (now) {
                spend += 1ll*now * ed[i].cost, ed[i].flow -= now;
                ed[i ^ 1].flow += now, used += now;
            }
            if (used == low) break;
        }
    }
    return used;
}

int cost_flow() {
    int flow = 0;
    while (dij(s, t)) {
        vis[t] = 1;
        while (vis[t]) {
            memset(vis, 0, sizeof(vis));
            flow += dfs(s, 1e9);
        }
        for (int i = 0; i <= t; i++) h[i] += dis[i];
    }
    return flow;
}

int main() {
    memset(head, -1, sizeof(head));
    int n, k;
    scanf("%d%d", &n, &k);
    s = n + k + 1;
    t = s+1;
    rep (i, 1, n) {
        add(s, i, 1, 0);
        rep (j, 1, k) {
            int x; scanf("%d", &x);
            add(i, n+j, 1, x);
        }
    }
    rep (i, 1, k) {
        int x; scanf("%d", &x);
        add(n+i, t, x, 0);
    }

    cost_flow();
    cout << spend;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5976kb

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: 5720kb

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: 126ms
memory: 6080kb

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: 3006ms
memory: 6816kb

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

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:


result: