标签:des style http color os io strong for
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 16054 | Accepted: 7185 |
Description
Input
Output
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
构图的时候把掌管有同一个猪圈的两个人由先到的向后到的引一条无限流量的边!
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"queue"
#include"algorithm"
using namespace std;
#define N 1500
#define min(a,b) (a<b?a:b)
const int inf=0x7fffffff;
int c[N][N],head[N],cnt;
int dis[N],q[N];
struct node
{
int u,v,w,next;
}map[N*100];
void add(int u,int v,int w)
{
map[cnt].u=u;
map[cnt].v=v;
map[cnt].w=w;
map[cnt].next=head[u];
head[u]=cnt++;
map[cnt].u=v;
map[cnt].v=u;
map[cnt].w=0;
map[cnt].next=head[v];
head[v]=cnt++;
}
int bfs(int n)
{
int s=0,t1,t2,x,v,i;
memset(dis,0,sizeof(dis));
t1=t2=0;
q[t1++]=s;
dis[s]=1;
while(t2<t1)
{
x=q[t2++];
for(i=head[x];i!=-1;i=map[i].next)
{
v=map[i].v;
if(map[i].w&&!dis[v])
{
dis[v]=dis[x]+1;
if(v==n)
return 1;
q[t1++]=v;
}
}
}
return 0;
}
int dfs(int s,int n,int lim)
{
int t=n,i,v,tmp,cost=0;
if(s==t)
return lim;
for(i=head[s];i!=-1;i=map[i].next)
{
v=map[i].v;
if(map[i].w&&dis[v]==dis[s]+1)
{
tmp=dfs(v,n,min(lim-cost,map[i].w));
if(tmp>0)
{
map[i].w-=tmp;
map[i^1].w+=tmp;
cost+=tmp;
if(cost==lim)
break;
}
else
dis[v]=-1;
}
}
return cost;
}
int dinic(int n)
{
int ans=0,s=0;
while(bfs(n))
ans+=dfs(s,n,inf);
return ans;
}
int main()
{
int b,q[N],i,j,k,l,n,m;
int a[N];
while(scanf("%d%d",&m,&n)!=-1)
{
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<=m;i++)
{
scanf("%d",&a[i]);
add(0,i,a[i]);
}
for(i=1;i<=n;i++)
{
scanf("%d",&q[i]); //客户拿的钥匙数目
for(j=1;j<=q[i];j++) //猪舍
{
scanf("%d",&k); //能打开的猪舍
c[i][j]=k;
add(k,m+i,a[k]);
}
scanf("%d",&b);
add(m+i,m+n+1,b);
for(j=1;j<i;j++) //寻找该客户之前的客户是否能打开同一个猪舍
{
int flag=0;
for(k=1;k<=q[j];k++) //某个客户能打开的猪舍
{
for(l=1;l<=q[i];l++)//第I个客户能打开的猪舍
{
if(c[j][k]==c[i][l])
{
add(j+m,i+m,inf); //建一条无限流量的边
flag=1;
break;
}
}
if(flag)
break;
}
}
}
printf("%d\n",dinic(n+m+1));
}
return 0;
}poj 1149 PIGS(网络流dinic),布布扣,bubuko.com
标签:des style http color os io strong for
原文地址:http://blog.csdn.net/u011721440/article/details/38386281