## 3894: 文理分科

3894: 文理分科

Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 490 Solved: 293
[Submit][Status][Discuss]
Description

1．如果第i行第秒J的同学选择了文科，则他将获得art[i][j]的满意值，如

2．如果第i行第J列的同学选择了文科，并且他相邻（两个格子相邻当且

3．如果第i行第j列的同学选择了理科，并且他相邻的同学全部选择了理

Input

Output

Sample Input

3 4

13 2 4 13

7 13 8 12

18 17 0 5

8 13 15 4

11 3 8 11

11 18 6 5

1 2 3 4

4 2 3 2

3 1 0 4

3 2 3 2

0 2 2 1

0 2 4 4
Sample Output

152
HINT

1表示选择文科，0表示选择理科，方案如下：

1 0 0 1

0 1 0 0

1 0 0 0

N,M<=100,读入数据均<=500

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
#define LL long long
#define id(i,j) ((i)*y-y+(j))
{
int s=0,f=1;char ch=getchar();
while(!('0'<=ch&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}
return s*f;
}
int n,S,T,x,y;
int be[30005],bn[280005],bv[280005],bl[280005],bw=1;
int ce[30005],cn[280005],cv[280005],cl[280005],cw=1;
void put(int u,int v,int l)
{cw++;cn[cw]=ce[u];ce[u]=cw;cv[cw]=v;cl[cw]=l;}
int dis[30005];
bool bfs(int s,int t)
{
for(int i=1;i<=n;i++)dis[i]=1000000,be[i]=ce[i];
dis[s]=1;
queue<int>q;
for(q.push(s);!q.empty();)
{int x=q.front();q.pop();
for(int i=be[x];i;i=bn[i])
if(bl[i]&&dis[bv[i]]>dis[x]+1)
{dis[bv[i]]=dis[x]+1;
q.push(bv[i]);
}
}
return dis[t]<=n;
}
int dinic(int x,int T,int f)
{
if(x==T)
return f;
int sum=0;
for(int &i=be[x];i&&f;i=bn[i])
if(bl[i]&&dis[bv[i]]==dis[x]+1)
{int s=dinic(bv[i],T,min(f,bl[i]));
f-=s;sum+=s;
bl[i]-=s,bl[i^1]+=s;
}
return sum;
}
int cut(int u,int v)
{
int sum=0;
for(int i=1;i<=cw;i++)bn[i]=cn[i],bl[i]=cl[i],bv[i]=cv[i];
while(bfs(u,v))
sum+=dinic(u,v,1000000000);
return sum;
}
int ans;
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
for(int i=1,k=0;i<=x;i++)
for(int j=1;j<=y;j++)
put(S,k,s);ans+=s;
put(k,S,0);
}
for(int i=1,k=0;i<=x;i++)
for(int j=1;j<=y;j++)
put(k,T,s);ans+=s;
put(T,k,0);
}
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
ans+=s;
put(S,n,s),put(n,S,0);
put(n,id(i,j),s),put(id(i,j),n,0);
if(i>1)put(n,id(i-1,j),s),put(id(i-1,j),n,0);
if(j>1)put(n,id(i,j-1),s),put(id(i,j-1),n,0);
if(i<x)put(n,id(i+1,j),s),put(id(i+1,j),n,0);
if(j<y)put(n,id(i,j+1),s),put(id(i,j+1),n,0);
}
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
ans+=s;
put(n,T,s),put(T,n,0);
put(id(i,j),n,s),put(n,id(i,j),0);
if(i>1)put(id(i-1,j),n,s),put(n,id(i-1,j),0);
if(j>1)put(id(i,j-1),n,s),put(n,id(i,j-1),0);
if(i<x)put(id(i+1,j),n,s),put(n,id(i+1,j),0);
if(j<y)put(id(i,j+1),n,s),put(n,id(i,j+1),0);
}
printf("%d\n",ans-cut(S,T));
//fclose(stdin);
//fclose(stdout);
return 0;
}



posted on 2016-06-14 16:13  wuyuhan  阅读(216)  评论(1编辑  收藏  举报