Title: Hdoj 4925 Apple Tree
Source: multi-university Training Contest 6
Test instructions: Give a matrix, then the number in each lattice is 2^ (the number of adjacent lattice), and then asked not to take the adjacent number, so that the number of the largest.
Analysis: This topic has two solutions, is a general solution, network flow, the other is to find the law, because the problem of data is regular, so you can find the law. That's what many people do.
The solution to the network flow is, in fact, a grid fetch number problem.
is hdoj 1569 Click to open the linked version, but the data range is increased. But the data is water. Down the same effect, the same code can be AC.
ans = sum-min cut
See the link above for details: Click to open the link
AC Code:
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include < algorithm> #include <vector> #include <queue>using namespace std; #define Del (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]; Build the hierarchy diagram 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)//The second condition guarantees {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)//optimize return F; int a = 0,ans=0; for (int &i=cur[o]; i<v[o].size (); i++)//Note front ' & ', very important optimization {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 of the mode of storage and mapping; F-=a; if (f==0)//attention optimization break; }} return ans; Optimized}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); Whilet--) {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;}
Hdoj 4925 Apple Tree "min cut"