poj1149 PIGS

来源:互联网 发布:西门子s7300编程软件 编辑:程序博客网 时间:2024/05/17 06:23

(http://www.elijahqi.win/2017/12/22/poj1149-pigs/)
Description
Mirko works on a pig farm that consists of M locked pig-houses and Mirko can’t unlock any pighouse because he doesn’t have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs.
All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold.
More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses.
An unlimited number of pigs can be placed in every pig-house.
Write a program that will find the maximum number of pigs that he can sell on that day.

Input
The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N.
The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000.
The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line):
A K1 K2 … KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, …, KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.

Output
The first and only line of the output should contain the number of sold pigs.

Sample Input

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

题意 有n个人要买猪 然后有m个猪圈 那么每个人买猪的同时都能打开若干个猪圈 然后这些猪圈都能互相的移动猪 然后每个人都有一个 购买的数量 同时 每个猪圈也有确切的数量 问题是 现在我需要让所有人买到最多的猪 不需要每个人都买满 问最多能买到多少猪
我一开始zz建图错了 样例还对了 以为自己会了其实方法是错误的因为

我两次打开不同的猪圈 如果这有一些猪圈是不同次打开的那么他们的猪不能互相转移 所以是错的

所以建图的方法就是我如果这个猪圈第一次 有人买他 就把他向那个人建容量为猪圈数的边 然后如果这个人想买的这个猪圈之前有人 买了 那么则从之前那个人向我建边 容量inf

最后每个人都向汇点建边 边权为那个人要买的猪的数量 然后跑最大流即可 这样就可以有效避免不同时打开的猪互相乱跑了

#include<queue>#include<cstdio>#include<cstring>#include<algorithm>#define N 2200#define inf 0x3f3f3f3fusing namespace std;inline char gc(){    static char now[1<<16],*S,*T;    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}    return *S++;}inline int read(){    int x=0;char ch=gc();    while(ch<'0'||ch>'9') ch=gc();    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}    return x;}struct node{    int y,z,next;}data[N*N];int num=1,level[N],h[N],n,m,T,v[N];inline void insert1(int x,int y,int z){    data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;    data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;}inline bool bfs(){    queue<int>q;memset(level,0,sizeof(level));level[0]=1;q.push(0);    while(!q.empty()){        int x=q.front();q.pop();        for (int i=h[x];i;i=data[i].next){            int y=data[i].y,z=data[i].z;if (level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if (y==T)return 1;        }    }return 0;}inline int dfs(int x,int s){    if (x==T) return s;int ss=s;    for (int i=h[x];i;i=data[i].next){        int y=data[i].y,z=data[i].z;        if (level[x]+1==level[y]&&z){            int xx=dfs(y,min(s,z));if(!xx) level[y]=0;            s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;        }     }return ss-s;}int last[N];int main(){    freopen("poj1149.in","r",stdin);    m=read();n=read();T=n+1;    for (int i=1;i<=m;++i) v[i]=read();     for (int i=1;i<=n;++i){        int tmp=read();for (int j=1;j<=tmp;++j){int from=read();if(!last[from]) insert1(0,i,v[from]);else insert1(last[from],i,inf);last[from]=i;}        int nn=read();insert1(i,T,nn);    } int ans=0;    while(bfs()) ans+=dfs(0,inf);printf("%d",ans);    return 0;}
原创粉丝点击