QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#196848 | #5616. Which Warehouse? | KKT89 | AC ✓ | 11ms | 3952kb | C++17 | 3.7kb | 2023-10-02 00:41:07 | 2023-10-02 00:41:07 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
inline double time() {
return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}
struct Primal_Dual{
const int INF=(1<<30);
struct edge{
int to,cap,cost,rev;
};
vector<vector<edge>> G;
vector<int> potential,min_cost,prevv,preve;
Primal_Dual(int V):G(V){}
void add_edge(int s,int t,int cap,int cost){
G[s].push_back((edge){t,cap,cost,(int)(G[t].size())});
G[t].push_back((edge){s,0,-cost,(int)(G[s].size()-1)});
}
int min_cost_flow(int s,int t,int f){
int V=G.size(); int res=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
potential.assign(V,0);
preve.assign(V,-1);
prevv.assign(V,-1);
while(f>0){
min_cost.assign(V,INF);
pq.push(make_pair(0,s));
min_cost[s]=0;
while(pq.size()){
auto p=pq.top(); pq.pop();
if(min_cost[p.second]<p.first)continue;
int i=-1;
for(auto &e:G[p.second]){
i++;
int nextcost=min_cost[p.second]+e.cost+potential[p.second]-potential[e.to];
if(e.cap>0 and min_cost[e.to]>nextcost){
min_cost[e.to]=nextcost;
prevv[e.to]=p.second;
preve[e.to]=i;
pq.push(make_pair(min_cost[e.to],e.to));
}
}
}
if(min_cost[t]==INF)return -1; // 流せない
for(int v=0;v<V;v++){
potential[v]+=min_cost[v];
}
int Addflow=f;
for(int v=t;v!=s;v=prevv[v]){
Addflow=min(Addflow,G[prevv[v]][preve[v]].cap);
}
f-=Addflow;
res+=Addflow*potential[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=Addflow;
G[v][e.rev].cap+=Addflow;
}
}
return res;
}
};
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,m; cin >> n >> m;
vector<vector<int>> v(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> v[i][j];
}
}
vector<vector<int>> d(n, vector<int>(n, 1e9));
for (int i = 0; i < n; ++i) {
d[i][i] = 0;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int dd; cin >> dd;
if (dd == -1) continue;
d[i][j] = dd;
}
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
}
}
}
Primal_Dual g(n+m+2);
int S = n+m, T = n+m+1;
for (int i = 0; i < m; ++i) {
g.add_edge(S, i, 1, 0);
}
for (int i = 0; i < n; ++i) {
g.add_edge(i+m, T, 1, 0);
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ll cost = 0;
for (int k = 0; k < n; ++k) {
cost += (ll)v[k][i]*d[k][j];
}
g.add_edge(i, j+m, 1, cost);
}
}
cout << g.min_cost_flow(S, T, m) << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3516kb
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: 1ms
memory: 3424kb
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: 3420kb
input:
1 1 10 0
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
2 1 0 10 0 1 1 0
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
2 1 12 10 0 1 1 0
output:
10
result:
ok single line: '10'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3480kb
input:
2 1 10 10 0 1 2 0
output:
10
result:
ok single line: '10'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3480kb
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: 3512kb
input:
2 2 12 10 11 13 0 1 1 0
output:
21
result:
ok single line: '21'
Test #9:
score: 0
Accepted
time: 11ms
memory: 3888kb
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: 11ms
memory: 3824kb
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: 1ms
memory: 3516kb
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: 3424kb
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: 1ms
memory: 3428kb
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: 3520kb
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: 3560kb
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: 1ms
memory: 3596kb
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: 3432kb
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: 1ms
memory: 3452kb
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: 3428kb
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: 3420kb
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: 0ms
memory: 3532kb
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: 2ms
memory: 3824kb
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: 4ms
memory: 3704kb
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: 0ms
memory: 3584kb
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: 4ms
memory: 3756kb
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: 3952kb
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: 6ms
memory: 3864kb
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: 3820kb
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: 4ms
memory: 3780kb
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: 7ms
memory: 3852kb
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: 9ms
memory: 3888kb
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'