标签:str return his tchar sdi cto amp define read
#include <bits/stdc++.h>
#define MAXN 1000001
#define mset(a,b) memset(a,b,sizeof(a))
#define inf 214700000
using namespace std;
struct Dinic{
struct Edge{
int to,w;
};
int T,S,vis[MAXN];
vector<int> mp[MAXN];
vector<Edge> e;
void addEdge(int a,int b,int c){
mp[a].push_back(e.size());
e.push_back((Edge){b,c});
mp[b].push_back(e.size());
e.push_back((Edge){a,c});
}
bool bfs(){
mset(vis,0);
queue<int> q;
q.push(S);
vis[S]=1;
while(!q.empty()){
int a=q.front();
if(a==T)
return true;
for(int i=0;i<mp[a].size();i++){
Edge now=e[mp[a][i]];
if(!vis[now.to]&&now.w){
vis[now.to]=vis[a]+1;
q.push(now.to);
}
}
q.pop();
}
return false;
}
int dfs(int d,int maxn){
int ans=0;
if(d==T)
return maxn;
if(!maxn)
return 0;
for(int i=0;i<mp[d].size();i++){
Edge now=e[mp[d][i]];
if(!~(vis[d]-vis[now.to])&&now.w){
int w=dfs(now.to,min(maxn-ans,now.w));
e[mp[d][i]].w-=w;
e[mp[d][i]^1].w+=w;
ans+=w;
if(ans==maxn)
return ans;
}
}
if(!ans)
vis[d]=-1;
return ans;
}
int GetAns(){
int ans=0;
while(bfs())
ans+=dfs(S,inf);
return ans;
}
};
struct IOer{
int ReadInt(){
char c=getchar();
bool f=false;
int a=0;
while(!isdigit(c)){
if(c==‘-‘)
f=true;
c=getchar();
}
while(isdigit(c)){
a=(a<<1)+(a<<3)+c-‘0‘;
c=getchar();
}
if(f)
a=~a+1;
return a;
}
IOer operator << (int a){
int s[100],top=0;
if(!a){
putchar(‘0‘);
return *this;
}
if(a<0){
putchar(‘-‘);
a=~a+1;
}
while(a){
s[top++]=a%10;
a/=10;
}
top--;
while(~top)
putchar(s[top--]+‘0‘);
return *this;
}
IOer operator << (char c){
putchar(c);
return *this;
}
IOer operator >> (int &a){
char c=getchar();
bool f=false;
a=0;
while(!isdigit(c)){
if(c==‘-‘)
f=true;
c=getchar();
}
while(isdigit(c)){
a=(a<<1)+(a<<3)+c-‘0‘;
c=getchar();
}
if(f)
a=~a+1;
return *this;
}
};
int main(){
int n,m;
IOer io;
Dinic d;
io>>n>>m;
d.S=1;d.T=n*m;
for(int i=1;i<=n;i++)
for(int j=1;j<m;j++)
d.addEdge((i-1)*m+j,(i-1)*m+j+1,io.ReadInt());
for(int i=1;i<n;i++)
for(int j=1;j<=m;j++)
d.addEdge((i-1)*m+j,i*m+j,io.ReadInt());
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
d.addEdge((i-1)*m+j,i*m+j+1,io.ReadInt());
io<<d.GetAns()<<‘\n‘;
return 0;
}
标签:str return his tchar sdi cto amp define read
原文地址:http://www.cnblogs.com/HC-LittleJian/p/7765447.html