1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 #include <cassert> 21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 24 //****************** 25 int abss(int a); 26 int lowbit(int n); 27 int Del_bit_1(int n); 28 int maxx(int a,int b); 29 int minn(int a,int b); 30 double fabss(double a); 31 void swapp(int &a,int &b); 32 clock_t __STRAT,__END; 33 double __TOTALTIME; 34 void _MS(){__STRAT=clock();} 35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 36 //*********************** 37 #define rint register int 38 #define fo(a,b,c) for(rint a=b;a<=c;++a) 39 #define fr(a,b,c) for(rint a=b;a>=c;--a) 40 #define mem(a,b) memset(a,b,sizeof(a)) 41 #define pr printf 42 #define sc scanf 43 #define ls rt<<1 44 #define rs rt<<1|1 45 typedef vector<int> VI; 46 typedef long long ll; 47 const double E=2.718281828; 48 const double PI=acos(-1.0); 49 //const ll INF=(1LL<<60); 50 const int inf=(1<<30); 51 const double ESP=1e-9; 52 const int mod=(int)1e9+7; 53 const int N=5500; 54 const int M=5e5+10; 55 56 class DINIC 57 { 58 public: 59 // const int MAXN=10004,MAXWAY=100005; 60 int n,way,max_flow,deep[N]; 61 int tot,head[N],cur[N]; 62 struct EDGE{ 63 int to,next; 64 int dis; 65 }edge[M]; 66 void Init(int n_) 67 { 68 tot=-1;//因为加反向边要^1,所以要从0开始; 69 n=n_; 70 max_flow=0; 71 for(int i=0;i<=n_;++i) 72 head[i]=-1; 73 } 74 void add(int from,int to,int V) 75 { 76 //正向 77 ++tot; 78 edge[tot].to=to; 79 edge[tot].dis=V; 80 edge[tot].next=head[from]; 81 head[from]=tot; 82 //反向 83 swap(from,to); 84 ++tot; 85 edge[tot].to=to; 86 edge[tot].dis=0; 87 edge[tot].next=head[from]; 88 head[from]=tot; 89 } 90 queue<int>q; 91 bool bfs(int s,int t) 92 { 93 for(int i=1;i<=n;++i) 94 deep[i]=inf; 95 while(!q.empty())q.pop(); 96 for(int i=1;i<=n;++i)cur[i]=head[i]; 97 deep[s]=0; 98 q.push(s); 99 100 while(!q.empty()) 101 { 102 int now=q.front();q.pop(); 103 for(int i=head[now];i!=-1;i=edge[i].next) 104 { 105 if(deep[edge[i].to]==inf&&edge[i].dis) 106 { 107 deep[edge[i].to]=deep[now]+1; 108 q.push(edge[i].to); 109 } 110 } 111 } 112 return deep[t]<inf; 113 } 114 int dfs(int now,int t,int limit) 115 { 116 if(!limit||now==t)return limit; 117 int flow=0,f; 118 for(int i=cur[now];i!=-1;i=edge[i].next) 119 { 120 cur[now]=i; 121 if(deep[edge[i].to]==deep[now]+1&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis)))) 122 { 123 flow+=f; 124 limit-=f; 125 edge[i].dis-=f; 126 edge[i^1].dis+=f; 127 if(!limit)break; 128 } 129 } 130 return flow; 131 } 132 void Dinic(int s,int t) 133 { 134 while(bfs(s,t)) 135 max_flow+=dfs(s,t,inf); 136 } 137 }G; 138 int a[N][N],color[N][N]; 139 140 int main() 141 { 142 int n,m; 143 while(~sc("%d%d",&n,&m)) 144 { 145 ll TOT=0; 146 for(int i=1;i<=n;++i) 147 for(int j=1;j<=m;++j) 148 sc("%d",&a[i][j]),TOT+=a[i][j]; 149 G.Init(n*m+2); 150 int S=n*m+1,T=n*m+2,cnt=0; 151 for(int i=1;i<=n;++i) 152 { 153 for(int j=1;j<=m;++j) 154 { 155 if(i&1) 156 { 157 if(j&1) 158 color[i][j]=1; 159 else 160 color[i][j]=0; 161 } 162 else 163 { 164 if(j&1) 165 color[i][j]=0; 166 else 167 color[i][j]=1; 168 } 169 } 170 } 171 for(int i=1;i<=n;++i) 172 { 173 for(int j=1;j<=m;++j) 174 { 175 ++cnt; 176 if(color[i][j]==0) 177 { 178 G.add(S,cnt,a[i][j]); 179 if(i>1)G.add(cnt,cnt-m,inf); 180 if(j>1)G.add(cnt,cnt-1,inf); 181 if(i<n)G.add(cnt,cnt+m,inf); 182 if(j<m)G.add(cnt,cnt+1,inf); 183 } 184 else 185 G.add(cnt,T,a[i][j]); 186 } 187 } 188 /* fo(i,1,n) 189 { 190 fo(j,1,m) 191 pr("%d ",color[i][j]); 192 pr("\n"); 193 }*/ 194 G.Dinic(S,T); 195 pr("%lld\n",TOT-G.max_flow); 196 } 197 return 0; 198 } 199 200 /**************************************************************************************/ 201 202 int maxx(int a,int b) 203 { 204 return a>b?a:b; 205 } 206 207 void swapp(int &a,int &b) 208 { 209 a^=b^=a^=b; 210 } 211 212 int lowbit(int n) 213 { 214 return n&(-n); 215 } 216 217 int Del_bit_1(int n) 218 { 219 return n&(n-1); 220 } 221 222 int abss(int a) 223 { 224 return a>0?a:-a; 225 } 226 227 double fabss(double a) 228 { 229 return a>0?a:-a; 230 } 231 232 int minn(int a,int b) 233 { 234 return a<b?a:b; 235 }
原文:https://www.cnblogs.com/--HPY-7m/p/11785832.html