zoukankan      html  css  js  c++  java
  • luoguP2774 方格取数问题

    传送门

    一开始还以为是个水题... 没想到是个藏于市井之中的dalao

    正常来讲想的肯定是贪心或者dp

    但是今天练的是网络流同时有行和列的限制或者中间空一行什么的

    然后就正难则反 求一下舍弃的点的最小值

    所以按邻接性染色 横纵坐标和为奇数的连源点 反之连汇点

    然后相邻点之间连一条inf表示两个必须切一个

    然后跑最小割就行

    Code:

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<queue>
      4 #include<iostream>
      5 #include<algorithm>
      6 #define ms(a,b) memset(a,b,sizeof a)
      7 #define rep(i,a,n) for(int i = a;i <= n;i++)
      8 #define per(i,n,a) for(int i = n;i >= a;i--)
      9 #define inf 2147483647
     10 using namespace std;
     11 typedef long long ll;
     12 ll read() {
     13     ll as = 0,fu = 1;
     14     char c = getchar();
     15     while(c < '0' || c > '9') {
     16         if(c == '-') fu = -1;
     17         c = getchar();
     18     }
     19     while(c >= '0' && c <= '9') {
     20         as = as * 10 + c - '0';
     21         c = getchar();
     22     }
     23     return as * fu;
     24 }
     25 const int N = 10005;
     26 const int M = 200005;
     27 //head
     28 int s = N-2,t = N-1;
     29 int head[N],nxt[M],mo[M],cnt = 1;
     30 ll cst[M];
     31 void _add(int x,int y,ll w) {
     32     mo[++cnt] = y;
     33     cst[cnt] = w;
     34     nxt[cnt] = head[x];
     35     head[x] = cnt;
     36 }
     37 void add(int x,int y,ll w) {
     38     if(x^y) _add(x,y,w),_add(y,x,0);
     39 }
     40 
     41 int dep[N],cur[N];
     42 bool bfs() {
     43     queue<int> q;
     44     memcpy(cur,head,sizeof cur);
     45     ms(dep,0),q.push(s),dep[s] = 1;
     46     while(!q.empty()) {
     47         int x = q.front();
     48         q.pop();
     49         for(int i = head[x];i;i = nxt[i]) {
     50             int sn = mo[i];
     51             if(!dep[sn] && cst[i]) {
     52                 dep[sn] = dep[x] + 1;
     53                 q.push(sn);
     54             }
     55         }
     56     }
     57     return dep[t];
     58 }
     59 
     60 ll dfs(int x,ll flow) {
     61     if(x == t || flow == 0ll) return flow;
     62     ll res = 0;
     63     for(int &i = cur[x];i;i = nxt[i]) {
     64         int sn = mo[i];
     65         if(dep[sn] == dep[x] + 1 && cst[i]) {
     66             ll d = dfs(sn,min(cst[i],flow - res));
     67             if(d) {
     68                 cst[i] -= d,cst[i^1] += d;
     69                 res += d;
     70                 if(res == flow) break;
     71             }
     72         }
     73     }
     74     if(res ^ flow) dep[x] = 0;
     75     return res;
     76 }
     77 
     78 ll DINIC() {
     79     ll ans = 0;
     80     while(bfs()) ans += dfs(s,inf);
     81     return ans;
     82 }
     83 
     84 int n,m;
     85 int idx(int x,int y) {return (x-1)*m+y;}
     86 
     87 int dx[] = {0,1,0,-1};
     88 int dy[] = {1,0,-1,0};
     89 bool check(int x,int y,int d) {
     90     x += dx[d],y += dy[d];
     91     if(x <= 0 || y <= 0 || x > n || y > m) return 0;
     92     return 1;
     93 }
     94 ll a[105][105],sum;
     95 
     96 int main() {
     97     n = read(),m = read();
     98     rep(i,1,n) rep(j,1,m) {
     99         int x = read();
    100         sum += x;
    101         if(i+j & 1) add(s,idx(i,j),x);
    102         else add(idx(i,j),t,x);
    103     }
    104     rep(i,1,n) rep(j,1,m) {
    105         if(!(i+j & 1)) continue;
    106         rep(d,0,3) if(check(i,j,d))
    107             add(idx(i,j),idx(i+dx[d],j+dy[d]),inf);
    108     }
    109     sum -= DINIC();
    110     printf("%lld
    ",sum);
    111     return 0;
    112 }

    附luogu大样例一组

    /*
    24 20
    97 306 251 297 261 997 30 956 547 477 479 874 260 686 299 810 640 75 515 121
    537 780 573 295 578 719 583 732 20 896 663 796 837 778 576 41 439 841 217 77
    190 536 945 364 291 753 940 981 626 687 705 676 516 332 172 648 180 322 120 557
    868 173 308 420 244 204 307 64 988 406 165 647 800 356 137 874 973 823 810 607
    529 154 23 159 652 322 927 905 46 695 420 336 752 427 50 186 214 699 29 333
    575 941 109 943 367 77 280 339 969 465 888 434 33 627 540 673 1 635 763 79
    354 616 607 739 119 268 952 977 814 459 505 486 641 176 858 316 655 708 183 767
    315 923 724 280 992 938 309 996 989 840 973 657 257 929 771 515 995 176 455 950
    294 749 272 44 207 512 797 792 3 538 226 646 819 920 51 839 45 195 753 784
    226 577 232 203 478 764 781 286 176 311 577 340 70 514 644 194 459 628 886 668
    923 324 564 934 670 651 167 283 994 374 473 765 686 159 185 955 121 316 882 350
    634 307 507 647 139 770 505 604 966 273 658 724 707 707 132 266 26 590 751 454
    625 730 842 141 527 376 451 626 261 258 892 478 122 271 211 860 870 812 122 17
    671 277 221 407 617 797 34 152 824 677 58 471 95 797 413 393 478 890 32 5
    750 742 405 519 358 388 229 232 10 804 326 447 292 339 61 646 691 383 549 178
    774 938 514 929 933 977 508 473 41 645 295 554 614 791 789 196 844 317 74 695
    676 994 936 384 320 838 835 639 745 720 736 204 364 467 924 99 251 555 28 609
    98 455 391 611 456 105 386 504 452 588 640 256 567 695 134 397 820 229 180 264
    966 552 425 164 451 634 863 722 929 681 846 907 903 405 837 189 49 456 717 717
    429 14 192 997 150 304 685 325 38 35 705 868 521 885 21 268 474 288 487 221
    429 786 813 345 287 666 933 976 946 494 675 783 999 74 169 76 271 84 927 85
    188 259 391 81 943 609 907 799 442 545 521 286 777 977 357 976 52 672 151 537
    276 142 927 723 563 528 187 522 950 581 770 69 860 179 200 798 753 83 438 522
    942 805 270 292 386 435 73 131 976 143 335 108 735 402 518 835 89 212 236 446
    */
    //126929
  • 相关阅读:
    1276
    1235
    1170
    Grids
    1137
    1127
    1076
    🍖Mysql三种模式的指定方式介绍
    🍖存储引擎介绍
    🍖MySQL库的详细操作
  • 原文地址:https://www.cnblogs.com/yuyanjiaB/p/10012782.html
Copyright © 2011-2022 走看看