学习博客
 
 
模板
 
#include<bits/stdc++.h>
using namespace std;
const int N=?,M=?;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],i,tot=1,s,t;
queue<int>q;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(?+1)<<2);
	memcpy(cur,h,(?+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
 
 
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N=373,M=10000;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],x,y,z,i,tot,n,m,s,t,T,sum,a[21][10],j,k;
queue<int>q;
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,sizeof(dep));
	memcpy(cur,h,sizeof(h));
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
int main(){
	scanf("%d",&T);
	for (;T--;){
		scanf("%d",&n);
		tot=1,memset(h,0,sizeof(h));
		for (i=1,sum=0;i<=n;sum+=a[i][8],i++)
			for (j=1;j<=9;j++) scanf("%d",&a[i][j]);
		s=371,t=372;
		for (i=1;i<=n;i++) ADD(s,i,a[i][8]);
		for (k=1;k<=n;k++)
			for (j=1;j<=7;j++)
				if (a[k][j])
					for (i=1;i<=a[k][9];i++) ADD(k,(i-1)*7+j+20,1);
		for (i=21;i<=370;i++) ADD(i,t,1);
		puts(sum==dinic()?"Yes":"No");
	}
}
 
 
#include<bits/stdc++.h>
using namespace std;
const int N=55002,M=155002;
struct node{
	int to,ne,c;
}e[M<<1];
int h[N],cur[N],i,s,t,ans,n,m,dep[N],tot=1,x,y,z;
queue<int>q;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(){
	q.push(s);
	memset(dep,63,(t+1)<<2);
	memcpy(cur,h,(t+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs()) ans+=dfs(s,1e9);
	return ans;
}
int main(){
	n=rd(),m=rd();
	s=0,t=n+m+1;
	for (i=1;i<=n;i++) ADD(s,i,rd());
	for (i=1;i<=m;i++){
		x=rd(),y=rd(),ans+=(z=rd());
		ADD(x,i+n,1e9);
		ADD(y,i+n,1e9);
		ADD(i+n,t,z);
	}
	printf("%d",ans-dinic());
}
 
 
#include<bits/stdc++.h>
using namespace std;
const int N=205,M=10205;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],x,y,z,i,tot=1,n,m,k,s,t,vis[102][102],l[102],c[102],j;
queue<int>q;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(t+1)<<2);
	memcpy(cur,h,(t+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
int main(){
	n=rd(),m=rd(),k=rd();
	for (i=1;i<=n;i++) l[i]=m-rd();
	for (i=1;i<=m;i++) c[i]=n-rd();
	for (i=1;i<=k;i++) x=rd(),y=rd(),vis[x][y]=1;
	for (i=1;i<=n;i++)
		for (j=1;j<=m;j++) l[i]-=vis[i][j],c[j]-=vis[i][j];
	for (i=1;i<=n;i++)
		if (l[i]<0) return puts("JIONG!"),0;
	for (i=1;i<=m;i++)
		if (c[i]<0) return puts("JIONG!"),0;
	t=n+m+1;
	for (i=1;i<=n;i++) ADD(s,i,l[i]);
	for (i=1;i<=m;i++) ADD(i+n,t,c[i]);
	for (i=1;i<=n;i++)
		for (j=1;j<=m;j++)
			if (!vis[i][j]) ADD(i,j+n,1);
	printf("%d",n*m-k-dinic());
}
 
 
#include<bits/stdc++.h>
using namespace std;
const int N=20002,M=120002;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],i,tot=1,s,t,n,m,x,y,sum;
queue<int>q;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(t+1)<<2);
	memcpy(cur,h,(t+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
int main(){
	n=rd(),m=rd();
	t=2*n+1;
	for (i=1;i<=n;i++) x=rd(),ADD(s,i,x),ADD(i+n,t,x),sum+=x;
	for (i=1;i<=m;i++) x=rd(),y=rd(),ADD(x,y+n,rd());
	printf("%d",sum-dinic());
}
 
 
![]()
 
#include<bits/stdc++.h>
using namespace std;
const int N=3005,M=2003005;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],i,tot=1,n,m,x,y,z,sum,s,t,k;
queue<int>q;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(t+2*m+1)<<2);
	memcpy(cur,h,(t+2*m+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,2e9);
	return ans;
}
int main(){
	n=rd(),t=n+1;
	for (i=1;i<=n;i++) sum+=(x=rd()),ADD(s,i,x);
	for (i=1;i<=n;i++) sum+=(x=rd()),ADD(i,t,x);
	m=rd();
	for (i=1;i<=m;i++){
		k=rd(),x=rd(),y=rd(),sum+=x+y;
		ADD(s,t+i,x),ADD(t+i+m,t,y);
		for (;k--;) z=rd(),ADD(t+i,z,2e9),ADD(z,t+i+m,2e9);
	}
	printf("%d",sum-dinic());
}
 
 
#include<bits/stdc++.h>
using namespace std;
const int N=403,M=20302;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],x,y,i,tot=1,n,s,t,a,b,j,k;
queue<int>q;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(t+1)<<2);
	memcpy(cur,h,(t+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
int main(){
	n=rd(),a=rd(),b=rd();
	t=2*n+a+b+1;
	for (i=1;i<=a;i++) ADD(s,2*n+i,1);
	for (i=1;i<=b;i++) ADD(2*n+a+i,t,1);
	for (i=1;i<=n;i++){
		x=rd(),y=rd(),ADD(i,i+n,1);
		for (j=1;j<=x;j++) k=rd(),ADD(2*n+k,i,1);
		for (j=1;j<=y;j++) k=rd(),ADD(i+n,2*n+a+k,1);
	}
	printf("%d",dinic());
}
 
 
#include<bits/stdc++.h>
using namespace std;
const int N=1005,M=250505;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],i,tot,s,t,sum,n,m,T,j,z,x,y,cas;
queue<int>q;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(t+1)<<2);
	memcpy(cur,h,(t+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
int main(){
	scanf("%d",&T);
	for (;T--;){
		tot=1,memset(h,0,sizeof(h));
		n=rd(),m=rd(),sum=0;
		for (i=1;i<=n;i++){
			z=rd(),x=rd(),y=rd();
			sum+=z;
			ADD(s,i,z);
			for (j=x;j<=y;j++) ADD(i,j+n,1);
		}
		t=n+501;
		for (i=1;i<=500;i++) ADD(i+n,t,m);
		printf("Case %d: %s\n\n",++cas,sum==dinic()?"Yes":"No");
	}
}
 
 
#include<bits/stdc++.h>
using namespace std;
const int N=105,M=2102;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],i,tot,ans1,ans2,cnt,s,t,sum,n,m,x[M],y[M],z[M],op[M],tmp[N],k,cost,c;
queue<int>q;
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(t+1)<<2);
	memcpy(cur,h,(t+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
int main(){
	while (~scanf("%d%d",&n,&m)){
		s=0,t=n+1,cnt=0,ans1=0,ans2=1e9;
		for (i=1;i<=n;i++) scanf("%d",&tmp[i]);
		for (i=1;i<=m;i++) scanf("%d%d%d%d",&x[i],&y[i],&z[i],&op[i]),cnt+=(op[i]>0);
		for (k=0;k<1<<cnt;k++){
			cost=0,tot=1,memset(h,0,(t+1)<<2),c=0;
			for (i=1;i<=n;i++) ADD(s,i,tmp[i]);
			for (i=1;i<=m;i++){
				if (op[i]<0) ADD(x[i],y[i],1e9),ADD(x[i],t,z[i]);
				if (op[i]==0) ADD(x[i],y[i],1e9);
				if (op[i]>0){
					if (k&(1<<c)) ADD(x[i],y[i],1e9),cost+=z[i];
					else ADD(x[i],y[i],1);
					c++;
				}
			}
			sum=dinic();
			if (sum>ans1) ans1=sum,ans2=cost;
			else if (sum==ans1) ans2=min(ans2,cost);
		}
		if (!ans1) puts("Poor Heaven Empire");
		else printf("%d %d\n",ans1,ans2);
	}
}
 
 
#include<bits/stdc++.h>
using namespace std;
const int N=5005,M=7502;
struct node{
	int to,ne,c;
}e[M<<1];
int dep[N],cur[N],h[N],i,tot,s,t,n,m,j,x,y,a[52][52],sum,dx[4]={0,0,1,-1},dy[4]={1,-1,0,0},k;
queue<int>q;
void add(int x,int y,int z){
	e[++tot]=(node){y,h[x],z};
	h[x]=tot;
}
void ADD(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(int s,int t){
	q.push(s);
	memset(dep,63,(t+1)<<2);
	memcpy(cur,h,(t+1)<<2);
	dep[s]=0;
	while (!q.empty()){
		int u=q.front();q.pop();
		for (int i=h[u],v;i;i=e[i].ne)
			if (dep[v=e[i].to]>1e9 && e[i].c) dep[v]=dep[u]+1,q.push(v);
	}
	return dep[t]<1e9;
}
int dfs(int u,int t,int lim){
	if (!lim || u==t) return lim;
	int flow=0,f;
	for (int i=cur[u],v;i;i=e[i].ne){
		cur[u]=i;
		if (dep[v=e[i].to]==dep[u]+1 && (f=dfs(v,t,min(lim,e[i].c)))){
			flow+=f,lim-=f;
			e[i].c-=f,e[i^1].c+=f;
			if (!lim) break;
		}
	}
	return flow;
}
int dinic(){
	int ans=0;
	while (bfs(s,t)) ans+=dfs(s,t,1e9);
	return ans;
}
int main(){
	while (~scanf("%d%d",&n,&m)){
		tot=1,memset(h,0,sizeof(h));
		sum=0;
		for (i=1;i<=n;i++)
			for (j=1;j<=m;j++) scanf("%d",&a[i][j]),sum+=a[i][j];
		t=n*m*2+1;
		for (i=1;i<=n;i++)
			for (j=1;j<=m;j++)
				if (!((i+j)&1)) ADD((i-1)*m+j+n*m,t,a[i][j]);
				else{
					ADD(s,(i-1)*m+j,a[i][j]);
					for (k=0;k<4;k++){
						x=i+dx[k],y=j+dy[k];
						if (x && y && x<=n && y<=m) ADD((i-1)*m+j,(x-1)*m+y+n*m,1e9);
					}
				}
		printf("%d\n",sum-dinic());
	}
}