标签:style blog http color io os java ar for
3 3 75 15 21 75 15 28 34 70 5
188
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 3000; 18 struct arc { 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = 0) { 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*10]; 27 int head[maxn],d[maxn],tot; 28 int S,T,q[maxn],hd,tail,n,m; 29 void add(int u,int v,int flow) { 30 e[tot] = arc(v,flow,head[u]); 31 head[u] = tot++; 32 e[tot] = arc(u,0,head[v]); 33 head[v] = tot++; 34 } 35 bool bfs() { 36 for(int i = 1; i <= T; i++) d[i] = 0; 37 d[S] = 1; 38 hd = tail = 0; 39 q[tail++] = S; 40 while(hd < tail) { 41 int u = q[hd++]; 42 for(int i = head[u]; ~i; i = e[i].next) { 43 if(e[i].flow && !d[e[i].to]) { 44 d[e[i].to] = d[u]+1; 45 q[tail++] = e[i].to; 46 } 47 } 48 } 49 return d[T]; 50 } 51 int dfs(int u,int low) { 52 int tmp = 0,f; 53 if(u == T || !low) return low; 54 for(int i = head[u]; ~i; i = e[i].next) { 55 if(d[e[i].to] == d[u]+1 && (f = dfs(e[i].to,min(low,e[i].flow)))) { 56 tmp += f; 57 e[i].flow -= f; 58 e[i^1].flow += f; 59 low -= f; 60 if(!low) break; 61 } 62 } 63 return tmp; 64 } 65 int main() { 66 while(~scanf("%d %d",&n,&m)) { 67 memset(head,-1,sizeof(head)); 68 int sum = tot = 0,o,ans = 0; 69 S = n*m+1; 70 T = n*m+2; 71 for(int i = 1; i <= n; i++) 72 for(int j = 1; j <= m; j++) { 73 scanf("%d",&o); 74 sum += o; 75 int tmp = (i-1)*m + j; 76 if((i+j)&1) { 77 add(S,tmp,o); 78 if(i > 1) add(tmp,tmp-m,INF); 79 if(j > 1) add(tmp,tmp-1,INF); 80 if(i < n) add(tmp,tmp+m,INF); 81 if(j < m) add(tmp,tmp+1,INF); 82 } else add(tmp,T,o); 83 } 84 while(bfs()) ans += dfs(S,INF); 85 printf("%d\n",sum-ans); 86 } 87 return 0; 88 }
标签:style blog http color io os java ar for
原文地址:http://www.cnblogs.com/crackpotisback/p/3973898.html