HDU 4859 coastline (maximum flow minimum cut)

Source: Internet
Author: User

A rare Chinese question will not be translated.

Enter the first behavior T, indicating that there are T groups of test data.
Each group of data starts with two integers n and M, indicating the map size. In the next n rows, each row contains a string of M, indicating a map. '.' indicates land, 'E' indicates shallow sea, and 'D' indicates deep sea.
[Technical Specification]
1. 1 <= T <= 100
2. 1 <= n, m <= 47

 

Assume that the map is a grid of N * m, some of which are land, some are shallow waters that can be filled, and some are infilled deep waters. The length of the coastline is defined as the edge length of a connecting block of land (which may contain the land filled in the shallow sea area). The two grids have at least one public edge, which is considered as China Unicom.
It is worth noting that the land areas of the city Z can be different, and the whole map is in the ocean. That is to say, the city Z is composed of islands, such as images, hawaii?
Your task is to fill in some shallow waters to make the coastline of all islands the longest.

Output the longest coastline and.

 

 

Solution:

At first, I thought it was greedy. Later I found that this planning problem was not feasible. The algorithm is the minimum cut, and then the Minimum Cut = maximum stream. The algorithm is the maximum stream.

Because the coastline must be the boundary between the sea and the shore (nonsense ~~~), Add a multi-layer Deep Ocean (lonely ~~) to the outer layer of the grid Diagram ~~~)

Apparently (I + J) % 2 = 0, the grid can only have a coastline with (I + J) % 2 = 1. A diagram of the two parts.

What we need to require is the most likely number of adjacent different pairs (<'D ','. '>), that is, to obtain as few adjacent identical pairs as possible (<'D', 'D'> or <'. ','. '> ).

Modeling, it seems that the drawing is more intuitive. Just draw the part, and EE connections.

Both the left and right traffic are INF, and the intermediate traffic is 1.

Obviously, the maximum flow is the least same logarithm.

The answer is sum-MF.

#pragma comment (linker,"/STACK:102400000,102400000")#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <algorithm>#include <cmath>#include <queue>#include <set>#include <map>#include <stack>using namespace std;#define mxn 2600#define mxe 26000#define inf 0x3f3f3f3fstruct SAP{int dis[mxn],pre[mxn],gap[mxn],arc[mxn],f[mxe],cap[mxe];int head[mxn],nxt[mxe],vv[mxe],e;void init(){e=0;memset(head,-1,sizeof(head));}void addedge(int u,int v,int c){vv[e]=v,cap[e]=c,nxt[e]=head[u],head[u]=e++;vv[e]=u,cap[e]=0,nxt[e]=head[v],head[v]=e++;}int max_flow(int s,int t,int n){int q[mxn],j,mindis,ans=0,ht=0,tl=1;int u,v,low;bool found,vis[mxn];memset(dis,0,sizeof(dis));memset(gap,0,sizeof(gap));memset(vis,0,sizeof(vis));memset(arc,0,sizeof(arc));memset(f,0,sizeof(f));q[0]=t,vis[t]=true,dis[t]=0,gap[0]=1;while(ht<tl){int u = q[ht++];for(int i=head[u];i!=-1;i=nxt[i]){v = vv[i];if(!vis[v]){vis[v]=true;dis[v]=dis[u]+1;q[tl++]=v;gap[dis[v]]++;arc[v]=head[v];}}}u=s;low=inf;pre[s]=s;while(dis[s]<n){found = false;for(int &i = arc[u];i!=-1;i=nxt[i]){if(dis[vv[i]]==dis[u]-1 && cap[i]>f[i]){found = true; v=vv[i];low = min(low, cap[i]-f[i]);pre[v]=u;u=v;if(u==t){while(u!=s){u=pre[u];f[arc[u]]+=low;f[arc[u]^1]-=low;}ans+=low;low=inf;}break;}}if(found) continue;mindis = n;for(int i=head[u];i!=-1;i=nxt[i]){if(mindis>dis[vv[i]] && cap[i]>f[i]){mindis = dis[vv[j=i]];arc[u]=i;}}if(--gap[dis[u]]==0) return ans;dis[u] = mindis+1;gap[dis[u]]++;u=pre[u];}return ans;}}sap;char maze[55][55];int dx[]={0,1,0,-1};int dy[]={1,0,-1,0};int main(){    int t,n,m,ca=0;    scanf("%d",&t);    while(t--){scanf("%d%d",&n,&m);for(int i=1;i<=n;++i) scanf("%s",maze[i]+1);for(int i=0;i<=n+1;++i) maze[i][0]=maze[i][m+1]=‘D‘;for(int j=0;j<=m+1;++j) maze[0][j]=maze[n+1][j]=‘D‘;sap.init();int src = (n+2)*(m+2);int des = src+1;for(int i=0;i<=n+1;++i){for(int j=0;j<=m+1;++j){int u = i*(m+2)+j;if((i^j)&1){if(maze[i][j]==‘D‘) sap.addedge(u,des,inf);if(maze[i][j]==‘.‘) sap.addedge(src,u,inf);}else {if(maze[i][j]==‘.‘) sap.addedge(u,des,inf);if(maze[i][j]==‘D‘) sap.addedge(src,u,inf);}for(int k=0;k<4;++k){int ii=i+dx[k];int jj=j+dy[k];if(ii<0 || jj<0 || ii>n+1||jj>m+1) continue;int u = i*(m+2)+j;int v = ii*(m+2)+jj;sap.addedge(u,v,1);}}}int mf = sap.max_flow(src,des,des+1);int sum = (n+1)*(m+2)+(n+2)*(m+1);printf("Case %d: %d\n",++ca,sum-mf);    }    return 0;}

 

HDU 4859 coastline (maximum flow minimum cut)

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.