hdoj 4925 Apple tree 最小割

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述hdoj 4925 Apple tree 最小割相关的知识,希望能为你提供帮助。
题目:hdoj 4925 Apple tree 
【hdoj 4925 Apple tree 最小割】

来源:2014 Multi-University Training Contest 6


题意:给出一个矩阵,然后每一个格子中的数是2^(相邻格子的个数),然后要求不能取相邻的数,让取得数最大。



分析:这个题目有两种解法,一共是通解。网络流,还有一种是找规律,因为题目中数据是有规律的,所以能够找规律。非常多人是这样做的。

以下给出网络流的解法,事实上就是一个方格取数问题。
就是hdoj 1569  点击打开链接  的版本号,仅仅只是数据范围增大了。只是数据水了。下来一样的效果。同样的代码能够ac。
ans = sum - 最小割
具体思路见上面链接:点击打开链接
AC代码:

#include < cstdio> #include < cstring> #include < iostream> #include < string> #include < algorithm> #include < vector> #include < queue> using namespace std; #define Del(a,b) memset(a,b,sizeof(a)) const int N = 10200; const int inf = 0x3f3f3f3f; int n,m; struct Node { int from,to,cap,flow; }; vector< int> v[N]; vector< Node> e; int vis[N]; //构建层次图 int cur[N]; void add_Node(int from,int to,int cap) { e.push_back((Node) { from,to,cap,0 }); e.push_back((Node) { to,from,0,0 }); int tmp=e.size(); v[from].push_back(tmp-2); v[to].push_back(tmp-1); } bool bfs(int s,int t) { Del(vis,-1); queue< int> q; q.push(s); vis[s] = 0; while(!q.empty()) { int x=q.front(); q.pop(); for(int i=0; i< v[x].size(); i++) { Node tmp = e[v[x][i]]; if(vis[tmp.to]< 0 & & tmp.cap> tmp.flow)//第二个条件保证 { vis[tmp.to]=vis[x]+1; q.push(tmp.to); } } } if(vis[t]> 0) return true; return false; } int dfs(int o,int f,int t) { if(o==t || f==0)//优化 return f; int a = 0,ans=0; for(int & i=cur[o]; i< v[o].size(); i++) //注意前面 ’& ‘,非常重要的优化 { Node & tmp = e[v[o][i]]; if(vis[tmp.to]==(vis[o]+1) & & (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))> 0) { tmp.flow+=a; e[v[o][i]^1].flow-=a; //存图方式 ans+=a; f-=a; if(f==0)//注意优化 break; } } return ans; //优化 }int dinci(int s,int t) { int ans=0; while(bfs(s,t)) { Del(cur,0); int tm=dfs(s,inf,t); ans+=tm; } return ans; } int solve(int i,int j) { int ans=1; if(i> 1) ans*=2; if(j> 1) ans*=2; if(i< n) ans*=2; if(j< m) ans*=2; return ans; } int id(int i,int j) { return (i-1)*m+j; } int main() { int T; scanf("%d",& T); while(T--) { scanf("%d%d",& n,& m); int s=0,t=m*n+1,x,sum=0; for(int i=1; i< =n; i++) { for(int j=1; j< =m; j++) { x=solve(i,j); sum+=x; if((i+j)%2) { add_Node(s,id(i,j),x); if(i> 1) add_Node(id(i,j),id(i-1,j),inf); if(j> 1) add_Node(id(i,j),id(i,j-1),inf); if(i< n) add_Node(id(i,j),id(i+1,j),inf); if(j< m) add_Node(id(i,j),id(i,j+1),inf); } else add_Node(id(i,j),t,x); } } printf("%d\n",sum-dinci(s,t)); for(int i=0; i< =t; i++) v[i].clear(); e.clear(); } return 0; }










    推荐阅读