QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#325309#5616. Which Warehouse?YuukiS#AC ✓12ms4372kbC++203.3kb2024-02-11 07:43:242024-02-11 07:43:24

Judging History

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

  • [2024-02-11 07:43:24]
  • 评测
  • 测评结果:AC
  • 用时:12ms
  • 内存:4372kb
  • [2024-02-11 07:43:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
const int INF = 1e9;

struct Edge
{
    int from, to, capacity, cost;
    Edge(int f, int t, int ca, int co) : from(f), to(t), capacity(ca), cost(co) {}
};

vector<vector<int>> adj, cost, capacity;

void shortest_paths(int n, int v0, vector<int>& d, vector<int>& p) {
    d.assign(n, INF);
    d[v0] = 0;
    vector<bool> inq(n, false);
    queue<int> q;
    q.push(v0);
    p.assign(n, -1);

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        inq[u] = false;
        for (int v : adj[u]) {
            if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) {
                d[v] = d[u] + cost[u][v];
                p[v] = u;
                if (!inq[v]) {
                    inq[v] = true;
                    q.push(v);
                }
            }
        }
    }
}

int min_cost_flow(int N, vector<Edge> edges, int K, int s, int t) {
    adj.assign(N, vector<int>());
    cost.assign(N, vector<int>(N, 0));
    capacity.assign(N, vector<int>(N, 0));
    for (Edge e : edges) {
        adj[e.from].push_back(e.to);
        adj[e.to].push_back(e.from);
        cost[e.from][e.to] = e.cost;
        cost[e.to][e.from] = -e.cost;
        capacity[e.from][e.to] = e.capacity;
    }

    int flow = 0;
    int cost = 0;
    vector<int> d, p;
    while (flow < K) {
        shortest_paths(N, s, d, p);
        if (d[t] == INF)
            break;

        // find max flow on that path
        int f = K - flow;
        int cur = t;
        while (cur != s) {
            f = min(f, capacity[p[cur]][cur]);
            cur = p[cur];
        }

        // apply flow
        flow += f;
        cost += f * d[t];
        cur = t;
        while (cur != s) {
            capacity[p[cur]][cur] -= f;
            capacity[cur][p[cur]] += f;
            cur = p[cur];
        }
    }

    if (flow < K)
        return -1;
    else
        return cost;
}


void solve() {
    int N, M; cin >> N >> M;
    vector<vi> amt(N, vi(M));
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++) {
            cin >> amt[i][j];
        }
    }
    vector<vi> am(N, vi(N));
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            cin >> am[i][j];
            if(am[i][j] == -1) am[i][j] = INF;
        }
    }
    for(int k = 0; k < N; k++)
        for(int i = 0; i < N; i++)
            for(int j = 0; j < N; j++)
                if(am[i][k] < INF && am[k][j] < INF)
                    am[i][j] = min(am[i][j], am[i][k] + am[k][j]);
    int numV = 2 + N + M;
    vector<Edge> edges;
    for(int i = 0; i < M; i++) {
        edges.emplace_back(0, i + 1, 1, 0);
        for(int j = 0; j < N; j++) {
            int cost = 0;
            for(int k = 0; k < N; k++) {
                cost += amt[k][i] * am[k][j];
            }
            edges.emplace_back(i + 1, j + M + 1, 1, cost);
        }
    }
    for(int i = 0; i < N; i++) {
        edges.emplace_back(i + M + 1, numV - 1, 1, 0);
    }
    cout << min_cost_flow(numV, edges, M, 0, numV - 1) << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int T = 1;
    // cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}

详细

Test #1:

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

input:

3 2
5 10
0 6
7 3
0 3 5
3 0 9
5 9 0

output:

58

result:

ok single line: '58'

Test #2:

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

input:

3 2
5 10
0 6
7 3
0 -1 5
-1 0 9
5 9 0

output:

124

result:

ok single line: '124'

Test #3:

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

input:

1 1
10
0

output:

0

result:

ok single line: '0'

Test #4:

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

input:

2 1
0
10
0 1
1 0

output:

0

result:

ok single line: '0'

Test #5:

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

input:

2 1
12
10
0 1
1 0

output:

10

result:

ok single line: '10'

Test #6:

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

input:

2 1
10
10
0 1
2 0

output:

10

result:

ok single line: '10'

Test #7:

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

input:

2 2
10 12
13 11
0 1
1 0

output:

21

result:

ok single line: '21'

Test #8:

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

input:

2 2
12 10
11 13
0 1
1 0

output:

21

result:

ok single line: '21'

Test #9:

score: 0
Accepted
time: 12ms
memory: 4372kb

input:

100 100
365 555 860 485 544 213 501 267 715 120 175 86 740 213 481 240 201 850 108 851 136 240 79 773 818 987 856 81 994 367 6 854 459 59 435 406 831 413 271 613 268 682 440 668 808 264 974 809 966 106 986 445 313 415 343 457 269 703 857 734 181 387 817 768 854 768 882 886 443 8 534 808 962 493 146 ...

output:

31843360

result:

ok single line: '31843360'

Test #10:

score: 0
Accepted
time: 12ms
memory: 4284kb

input:

100 100
371 169 496 501 256 720 700 567 201 380 805 166 541 813 431 334 195 581 1 853 252 696 804 782 485 331 380 843 538 504 417 366 375 107 255 572 639 293 257 487 856 514 620 398 926 737 39 731 635 239 769 776 544 768 563 47 677 260 388 277 28 866 99 106 340 916 391 392 217 670 963 822 449 732 15...

output:

32792136

result:

ok single line: '32792136'

Test #11:

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

input:

15 1
265
382
436
518
302
299
301
14
365
635
680
538
274
968
345
0 7 16 22 84 69 11 23 74 80 49 37 47 7 20
24 0 67 87 12 77 95 73 91 88 65 4 63 48 97
74 10 0 8 48 93 81 22 91 89 30 15 44 73 72
80 3 19 0 30 10 44 62 80 86 47 31 6 76 42
32 70 86 47 0 10 28 60 52 57 34 32 80 70 98
96 44 40 4 47 0 48 32 ...

output:

104227

result:

ok single line: '104227'

Test #12:

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

input:

15 2
265 0
382 0
436 0
518 0
302 0
299 0
301 0
14 0
365 0
635 0
680 0
538 0
274 392
968 0
345 0
0 7 16 22 84 69 11 23 74 80 49 37 47 7 20
24 0 67 87 12 77 95 73 91 88 65 4 63 48 97
74 10 0 8 48 93 81 22 91 89 30 15 44 73 72
80 3 19 0 30 10 44 62 80 86 47 31 6 76 42
32 70 86 47 0 10 28 60 52 57 34 32...

output:

111283

result:

ok single line: '111283'

Test #13:

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

input:

15 2
265 0
382 0
436 0
518 0
302 0
299 0
301 0
14 0
365 0
635 0
680 0
538 0
274 393
968 0
345 0
0 7 16 22 84 69 11 23 74 80 49 37 47 7 20
24 0 67 87 12 77 95 73 91 88 65 4 63 48 97
74 10 0 8 48 93 81 22 91 89 30 15 44 73 72
80 3 19 0 30 10 44 62 80 86 47 31 6 76 42
32 70 86 47 0 10 28 60 52 57 34 32...

output:

111294

result:

ok single line: '111294'

Test #14:

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

input:

10 10
775 944 857 998 921 239 887 998 520 870
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 -1 -1 -1 -1 -1 -1 -1 -1
1 0 1 -1 -1 -1 -1 -1 -1 -1
-1 1 0 1 -1 -1 -1 -...

output:

30425

result:

ok single line: '30425'

Test #15:

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

input:

10 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
264 378 978 85 415 701 716 924 613 794
0 1 -1 -1 -1 -1 -1 -1 -1 -1
1 0 1 -1 -1 -1 -1 -1 -1 -1
-1 1 0 1 -1 -1 -1 -1...

output:

18542

result:

ok single line: '18542'

Test #16:

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

input:

11 11
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
513 990 883 755 236 485 72 167 753 764 891
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 2 -1 -1 -1 -1 -1 -1 -1 -1 -1
...

output:

22318

result:

ok single line: '22318'

Test #17:

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

input:

9 4
10 0 10 0
0 0 0 0
10 10 0 0
0 0 0 0
10 10 10 10
0 0 0 0
0 0 10 10
0 0 0 0
0 10 0 10
0 1 -1 1 -1 -1 -1 -1 -1
1 0 1 -1 1 -1 -1 -1 -1
-1 1 0 -1 -1 1 -1 -1 -1
1 -1 -1 0 1 -1 1 -1 -1
-1 1 -1 1 0 1 -1 1 -1
-1 -1 1 -1 1 0 -1 -1 1
-1 -1 -1 1 -1 -1 0 1 -1
-1 -1 -1 -1 1 -1 1 0 1
-1 -1 -1 -1 -1 1 -1 1 0

output:

120

result:

ok single line: '120'

Test #18:

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

input:

30 1
877
703
970
927
405
582
743
582
471
425
0
373
35
729
510
406
408
824
48
521
969
333
110
4
743
825
438
198
919
638
0 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 0 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 0 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2...

output:

15716

result:

ok single line: '15716'

Test #19:

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

input:

3 3
10 13 13
1 1 1
1 3 2
0 1 1
1 0 1
1 1 0

output:

28

result:

ok single line: '28'

Test #20:

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

input:

4 4
32 63 1 1
32 1 63 1
32 1 1 63
1 1 1 1
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0

output:

105

result:

ok single line: '105'

Test #21:

score: 0
Accepted
time: 1ms
memory: 3772kb

input:

54 31
195 128 289 229 864 989 739 721 834 811 351 767 836 90 61 852 303 298 227 597 364 940 644 884 391 680 140 745 180 516 62
17 740 389 950 253 495 493 794 913 421 426 3 599 16 863 194 927 241 705 365 662 459 144 864 585 477 520 744 84 88 91
12 651 496 180 44 427 528 83 243 342 694 640 225 250 173...

output:

5921761

result:

ok single line: '5921761'

Test #22:

score: 0
Accepted
time: 4ms
memory: 3908kb

input:

98 51
326 189 769 769 400 404 255 574 713 737 254 903 916 8 474 131 452 690 684 367 523 87 45 127 978 381 1000 65 483 640 370 127 778 502 565 847 842 334 864 80 500 588 74 731 617 947 253 900 801 320 288
155 557 406 453 3 430 140 321 751 856 303 809 76 649 741 338 143 172 439 185 531 554 162 321 79 ...

output:

16031302

result:

ok single line: '16031302'

Test #23:

score: 0
Accepted
time: 3ms
memory: 4192kb

input:

78 47
760 1 338 479 348 99 487 599 222 395 256 786 340 211 596 234 616 819 93 440 23 124 110 356 225 248 985 63 185 396 657 446 859 246 622 894 861 465 847 78 694 226 690 669 964 767 383
457 642 707 971 617 709 40 391 216 192 873 296 342 934 987 386 501 156 304 990 157 313 103 867 964 357 179 440 27...

output:

12505597

result:

ok single line: '12505597'

Test #24:

score: 0
Accepted
time: 1ms
memory: 3792kb

input:

51 34
713 158 954 38 368 362 599 594 974 909 941 402 304 161 377 674 97 269 326 342 177 509 725 663 856 391 416 537 3 708 960 812 879 824
182 997 4 968 62 497 808 496 416 677 397 23 89 581 292 735 510 711 712 740 512 883 672 777 419 994 580 313 664 703 913 41 715 630
351 63 591 912 655 126 756 816 2...

output:

7358556

result:

ok single line: '7358556'

Test #25:

score: 0
Accepted
time: 3ms
memory: 3908kb

input:

81 49
934 168 504 321 472 177 942 501 989 147 65 365 451 377 151 285 252 773 142 793 867 660 344 157 803 675 341 700 619 141 295 843 726 354 792 632 856 637 39 252 290 738 514 787 198 314 174 388 414
812 465 337 31 634 184 421 891 365 77 124 596 246 241 244 84 619 15 862 393 601 48 457 910 132 923 4...

output:

11769727

result:

ok single line: '11769727'

Test #26:

score: 0
Accepted
time: 10ms
memory: 4020kb

input:

97 92
93 724 989 467 672 419 405 910 901 716 700 948 735 177 160 66 144 575 554 252 737 383 16 584 212 340 612 568 877 948 857 738 327 822 445 405 258 902 442 226 162 311 671 877 460 686 988 942 25 774 114 513 805 590 825 526 177 275 507 549 844 203 165 730 952 896 718 400 145 324 928 430 480 167 47...

output:

29083791

result:

ok single line: '29083791'

Test #27:

score: 0
Accepted
time: 5ms
memory: 3976kb

input:

94 58
224 612 765 809 378 809 921 310 546 850 731 31 402 713 626 786 239 410 915 644 47 623 835 950 582 855 73 912 751 795 610 980 527 901 782 282 542 925 680 804 47 569 908 397 327 304 528 98 554 907 5 341 496 56 743 194 847 757
783 385 858 213 34 399 511 946 362 776 207 600 570 997 294 442 959 445...

output:

17071864

result:

ok single line: '17071864'

Test #28:

score: 0
Accepted
time: 6ms
memory: 4288kb

input:

97 63
79 194 999 538 496 55 482 337 689 83 909 81 324 732 780 35 90 935 897 597 262 22 396 861 549 886 572 587 738 140 732 593 70 68 832 882 516 110 166 404 859 669 994 139 36 829 370 195 171 417 984 698 283 134 726 320 266 543 691 965 260 397 4
573 211 115 406 554 707 468 855 326 412 601 732 658 34...

output:

21270663

result:

ok single line: '21270663'

Test #29:

score: 0
Accepted
time: 3ms
memory: 3928kb

input:

78 51
378 695 249 77 810 255 912 972 475 670 245 865 928 765 442 938 675 120 671 830 74 345 319 313 386 442 762 172 952 443 574 480 487 276 406 871 779 431 445 400 492 542 451 533 942 356 230 898 109 755 116
668 278 409 5 12 753 263 517 151 691 281 751 403 815 539 85 360 348 349 611 824 914 85 28 54...

output:

14410314

result:

ok single line: '14410314'

Test #30:

score: 0
Accepted
time: 3ms
memory: 4048kb

input:

92 73
307 719 300 137 354 145 312 115 182 991 372 435 479 964 960 783 350 824 607 542 992 479 113 399 471 451 610 607 955 950 912 837 935 904 922 612 320 961 579 981 977 467 382 21 601 935 340 250 534 379 808 518 403 149 532 430 229 567 300 177 595 455 568 872 179 825 432 319 314 763 768 213 483
205...

output:

22136855

result:

ok single line: '22136855'

Test #31:

score: 0
Accepted
time: 10ms
memory: 4256kb

input:

96 90
93 974 157 721 351 410 187 56 744 580 607 900 83 954 469 23 205 475 731 191 418 968 850 428 683 704 414 683 795 342 24 347 593 441 493 259 728 552 631 799 753 568 785 221 978 59 976 240 216 7 197 8 216 104 624 708 4 43 782 742 881 168 170 649 820 586 842 729 548 567 848 222 491 383 201 223 951...

output:

31498263

result:

ok single line: '31498263'