Graph theory (the maximum weight independent point set of two graphs): COGS 2051. Sword of the King

Source: Internet
Author: User

2051. Sword of the King

★★★☆ input file: Excalibur.in output file: Excalibur.out Simple comparison
Time limit: 1 s memory limit:

"Title description"

This was before Altolia Pandragon became the hero, she was about to pull out the stone sword to become King Arthur, before she went to collect some gems.

The gems are arranged in a n*m grid with a piece of i,j in each grid, and Altolia Pandragon can choose its own starting point.

The start time is 0 seconds. The following operations, executed sequentially per second

1. At the beginning of the first second, Altolia Pandragon on the square (x, y), she can take away (x, y) of the gem.

2. In even seconds, the gems around the Altolia Pandragon four will disappear.

3. Joaltolia Pandragon at the beginning of the first time in the square (x, y), the i+1 second can be immediately moved to (X+1,y), (x,y+1), (x-1,y) or (x,y-1), or can stay on (x, y).

Ask Altolia Pandragon to get the most valuable gems

"Input Format"

The first line gives the number of characters n,m. The n,m are less than or equal to 100, and the value of the gem will not exceed 10000. The following N rows M column is used to describe the number matrix

"Output Format"

How many value gems can be obtained from the output

"Sample Input"

2 2

1 2

2 1

"Sample Output"
4
Source

Yiu Jinyu's original problem, modified

  

You can find some details that you don't need to think about.

The Hubo paper has a detailed answer to this question.

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 using namespacestd;6 Const intmaxn=10010;7 Const intmaxm=800010;8 Const intinf=1000000000;9 intCNT,TOT,FIR[MAXN],FRON[MAXN],DIS[MAXN];Ten intTO[MAXM],NXT[MAXM],GAP[MAXN],PATH[MAXN]; One intcap[maxm];queue<int>Q; A  - structmax_flow{ -     voidInit (inttot_=0){ theTot=tot_;cnt=1; -memset (Fir,0,sizeof(FIR)); -memset (DIS,0,sizeof(DIS)); -Memset (Gap,0,sizeof(GAP)); +     } -      +     voidAddintAintBintc) { Anxt[++cnt]=Fir[a]; atfir[a]=CNT; -cap[cnt]=C; -to[cnt]=b; -     } -      -     voidAddedge (intAintBintc) { in Add (a,b,c); -Add (B,a,0); to     } +      -     BOOLBFS (intSintt) { thedis[t]=1; Q.push (t); *          while(!Q.empty ()) { $             intx=Q.front (); Q.pop ();Panax Notoginseng              for(intI=fir[x];i;i=Nxt[i]) -                 if(!Dis[to[i]]) { thedis[to[i]]=dis[x]+1; + Q.push (To[i]); A                 } the         } +         returnDis[s]; -     } $      $     int(intSintTint&p) { -         intf=INF; -          while(p!=R) { thef=min (f,cap[path[p]]); -p=to[path[p]^1];Wuyi}p=T; the          while(p!=s) { -cap[path[p]]-=F; Wucap[path[p]^1]+=F; -p=to[path[p]^1]; About         } $         returnF; -     } -      -     intISAP (intSintt) { A         if(! BFS (s,t))return 0; +          for(inti=s;i<=t;i++) fron[i]=Fir[i]; the          for(inti=s;i<=t;i++) gap[dis[i]]+=1; -         intp=s,ret=0; $          while(dis[s]<=tot) { the             if(p==t) ret+=(s,t,p); the              the              for(int&i=fron[p];i;i=Nxt[i]) the                 if(cap[i]&&dis[p]==dis[to[i]]+1){ -path[p=to[i]]=i; in                      Break; the                 } the              About             if(!Fron[p]) { the                 if(--gap[dis[p]]==0) the                      Break; the                 intmin=tot; +                  for(intI=fir[p];i;i=Nxt[i]) -                     if(Cap[i]) min=min (Min,dis[to[i]]); thegap[dis[p]=min+1]+=1; fron[p]=Fir[p];Bayi                 if(p!=s) p=to[path[p]^1];  the             }     the         } -         returnret; -     } the }isap; the  the intn,m; the intMain () { -Freopen ("excalibur.in","R", stdin); theFreopen ("Excalibur.out","W", stdout); thescanf"%d%d",&n,&m); theIsap. Init (n*m+1);94     ints=0, t=n*m+1, ans=0; the      for(intI=1; i<=n;i++) the          for(intj=1, v;j<=m;j++){ thescanf"%d", &v); ans+=v;98             intId= (I-1) *m+J; About             if((i+j)%2){ - Isap.addedge (s,id,v);101                 if(id-m>=1) Isap.addedge (id,id-m,inf);102                 if(id+m<=n*m) Isap.addedge (id,id+m,inf);103                 if(id%m&&id+1<=n*m) Isap.addedge (id,id+1, INF);104                 if((id-1)%m&&id-1>=1) Isap.addedge (id,id-1, INF); the             }106             ElseIsap.addedge (id,t,v);107         }108printf"%d\n", ans-ISAP. ISAP (s,t));109     return 0; the}

Graph theory (the maximum weight independent point set of two graphs): COGS 2051. Sword of the King

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.