比赛 20160418x 评测结果 AAAAAAAEEE
题目名称 wifi 最终得分 70
用户昵称 咸鱼二号 运行时间 2.425 s
代码语言 C++ 内存使用 138.22 MiB
提交时间 2016-04-18 16:06:21
显示代码纯文本
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<utility>
#include<cstdlib>
#include<iomanip>	//cout<<setiosflags(ios::fixed)<<setprecision(2);
#include<ctime> //double a=(double)clock();	cout<<a<<endl;
#include<vector>
#include<queue>
using namespace std;
const int INF=(1LL<<31)-1;
const int maxn=65;
inline int read(){
	int x=0,ff=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')ff=-1; ch=getchar();}
	while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
	return ff*x;
}
inline int mymin(int xx,int yy)
{if(xx<yy)return xx;return yy;}
int N,M,a,b,S,T,t1,t2,t3,t4,t,ans=0;
inline int get_id(int xx,int yy)
{return (xx-1)*M+yy;}
int lin[50010],len=0;
struct edge{
	int y,next,v;
}e[12000010];
inline void insert(int xx,int yy,int vv){
	e[++len].next=lin[xx];
	lin[xx]=len;
	e[len].y=yy;
	e[len].v=vv;
}
inline void ins(int xx,int yy,int vv)
{insert(xx,yy,vv),insert(yy,xx,0);}
int level[50010],q[50010],head,tail;
bool makelevel(){
	memset(level,-1,sizeof(level));
	head=tail=0;
	q[head]=S;level[S]=0;
	for(;head<=tail;head++)
		for(int i=lin[q[head]];i;i=e[i].next)
			if(e[i].v&&level[e[i].y]==-1){
				level[e[i].y]=level[q[head]]+1;
				q[++tail]=e[i].y;
			}
	return level[T]!=-1;
}
int max_flow(int x,int flow){
	if(x==T)
		return flow;
	int maxflow=0,d;
	for(int i=lin[x];i&&flow>maxflow;i=e[i].next)
		if(e[i].v&&level[e[i].y]==level[x]+1){
			d=max_flow(e[i].y,mymin(e[i].v,flow-maxflow));
			if(d){
				maxflow+=d;
				e[i].v-=d;
				if(i&1)
					e[i+1].v+=d;
				else
					e[i-1].v+=d;
			}
		}
	if(!maxflow)
		level[x]=-1;
	return maxflow;
}
void Dinic(){
	int d;
	while(makelevel()){
		d=1;
		while(d){
			d=max_flow(S,INF);
			ans+=d;
		}
	}
}
int main(){
	freopen("wifi.in","r",stdin);
	freopen("wifi.out","w",stdout);
	N=read(),M=read(),a=read(),b=read();
	S=a+b+N*M*2+1,T=S+1;
	for(int i=1;i<=N;i++)
		for(int j=1;j<=M;j++){
			t=read();
			ins(get_id(i,j),get_id(i,j)+N*M,t);
		}
	for(int I=1;I<=a;I++){
		t1=read(),t2=read(),t3=read(),t4=read(),t=read();
		ins(S,I+N*M*2,t);
		for(int i=t1;i<=t3;i++)
			for(int j=t2;j<=t4;j++)
				ins(I+N*M*2,get_id(i,j),INF);
	}
	for(int I=1;I<=b;I++){
		t1=read(),t2=read(),t3=read(),t4=read(),t=read();
		ins(I+N*M*2+a,T,t);
		for(int i=t1;i<=t3;i++)
			for(int j=t2;j<=t4;j++)
				ins(get_id(i,j)+N*M,I+N*M*2+a,INF);
	}
	Dinic();
	printf("%d\n",ans);
	return 0;
}