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;}
- poj1149 PIGS
- poj1149(pigs)
- POJ1149:PIGS
- poj1149 - PIGS
- poj1149 PIGS
- poj1149 PIGS
- poj1149 PIGS
- POJ1149 PIGS
- POJ1149 PIGS
- poj1149 pigs
- poj1149 PIGS
- POJ1149 PIGS
- POJ1149 PIGS
- POJ1149-PIGS
- poj1149 PIGS
- POJ1149 PIGS 题解
- POJ1149 PIGS(最大流)
- poj1149 最大流 PIGS
- linux防火墙开放特定端口
- 《SpringCloud微服务实战》学习笔记之配置中心Config
- Quartz任务调度存储与持久化操作配置详细解析(1)
- 39条常见的Linux系统简单面试题
- java 面向对象 和 四大特性
- poj1149 PIGS
- 报表导出测试 check point
- Mac下配置Gradle
- 快速简单的了解java List,Map,List<Map>的用法
- 几道JAVA和分布式系统面试题总结
- selenium常用操作
- 微信扫一扫功能的实现
- 设备 naa.6006016004102900751132ac8de3e211 性能降低。
- Log4j控制台输出乱码问题