PIGS

Time Limit: 1000ms
Memory Limit: 10000KB

This problem will be judged on PKU.
64-bit integer(整数) IO format: %lld      Java class name: Main

 
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 arives, 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

  1. 3 3
  2. 3 1 10
  3. 2 1 2 2
  4. 2 1 3 3
  5. 1 2 6

Sample Output

  1. 7
      
      建模题,这里需要注意对空间的优化。
      题意:迈克有个养猪场,养猪场里有M个猪圈,每个猪圈都上了锁。迈克没有钥匙,而要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,要买一定数量的猪。当每个顾客来时,将有钥匙的猪圈全部打开,从中挑出一些买走,然后迈克可以重新分配这些猪圈里面的猪。当顾客离开后,门又被锁上。问迈克最多可以卖多少猪。
      建模:先从源点给每个猪圈连一条边,容量是猪圈中猪的头数。这时再添加顾客,对于每一个顾客,查找他要开的每一个猪圈,如果他要开猪圈A,那么现在分情况讨论:
      <1>若以前(先后顺序,时间上的)没有顾客开过A猪圈,那么就连一条A到这个顾客的边,容量为INF,同时标记这个人为这个猪圈的“开启者”
      <2>若有,则将A的“开启者”连到这个人,容量为INF
      最后每个顾客连边到汇点,容量为各自的需求,接着跑一遍最大流就可以啦,这里我用了ISAP算法
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <queue>
  5.  
  6. using namespace std;
  7. const int INF=;
  8. const int maxn=,maxm=;
  9. int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn],used[maxn];
  10.  
  11. void addedge(int a,int b,int c)
  12. {
  13. nxt[++cnt]=fir[a];
  14. to[cnt]=b;
  15. cap[cnt]=c;
  16. fir[a]=cnt;
  17. }
  18.  
  19. bool BFS(int S,int T)
  20. {
  21. memset(dis,,sizeof(dis));
  22. dis[T]=;
  23. queue<int>q;q.push(T);
  24. while(!q.empty())
  25. {
  26. int node=q.front();q.pop();
  27. for(int i=fir[node];i;i=nxt[i])
  28. {
  29. if(dis[to[i]])continue;
  30. dis[to[i]]=dis[node]+;
  31. q.push(to[i]);
  32. }
  33. }
  34. return dis[S];
  35. }
  36. int fron[maxn];
  37. int ISAP(int S,int T)
  38. {
  39. if(!BFS(S,T))
  40. return ;
  41. for(int i=;i<=T;i++)++gap[dis[i]];
  42. int p=S,ret=;
  43. memcpy(fron,fir,sizeof(fir));
  44. while(dis[S]<=T)
  45. {
  46. if(p==T){
  47. int f=INF;
  48. while(p!=S){
  49. f=min(f,cap[path[p]]);
  50. p=to[path[p]^];
  51. }
  52. p=T;ret+=f;
  53. while(p!=S){
  54. cap[path[p]]-=f;
  55. cap[path[p]^]+=f;
  56. p=to[path[p]^];
  57. }
  58. }
  59. int &ii=fron[p];
  60. for(;ii;ii=nxt[ii]){
  61. if(!cap[ii]||dis[to[ii]]+!=dis[p])
  62. continue;
  63. else
  64. break;
  65. }
  66. if(ii){
  67. p=to[ii];
  68. path[p]=ii;
  69. }
  70. else{
  71. if(--gap[dis[p]]==)break;
  72. int minn=T+;
  73. for(int i=fir[p];i;i=nxt[i])
  74. if(cap[i])
  75. minn=min(minn,dis[to[i]]);
  76. gap[dis[p]=minn+]++;
  77. fron[p]=fir[p];
  78. if(p!=S)
  79. p=to[path[p]^];
  80. }
  81. }
  82. return ret;
  83. }
  84.  
  85. void Init()
  86. {
  87. memset(fir,,sizeof(fir));
  88. memset(used,,sizeof(used));
  89. cnt=;
  90. }
  91. int main()
  92. {
  93. int n,m,num,k,need;
  94. while(~scanf("%d%d",&m,&n))
  95. {
  96. Init();
  97. for(int i=;i<=m;i++){
  98. scanf("%d",&num);
  99. addedge(,i,num);
  100. addedge(i,,);
  101. }
  102. for(int i=m+;i<=m+n;i++){
  103. scanf("%d",&k);
  104. while(k--){
  105. scanf("%d",&num);
  106. if(used[num]){
  107. addedge(used[num],i,INF);
  108. addedge(i,used[num],);
  109. }
  110. else{
  111. used[num]=i;
  112. addedge(num,i,INF);
  113. addedge(i,num,);
  114. }
  115.  
  116. }
  117. scanf("%d",&need);
  118. addedge(i,n+m+,need);
  119. addedge(n+m+,i,);
  120. }
  121. printf("%d\n",ISAP(,n+m+));
  122. }
  123. return ;
  124. }

网络流(最大流):POJ 1149 PIGS的更多相关文章

  1. poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新

    题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...

  2. POJ 1149 PIGS(Dinic最大流)

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20738   Accepted: 9481 Description ...

  3. POJ 1149 - PIGS - [最大流构图]

    Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...

  4. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  5. POJ 1149 PIGS(最大流)

    Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...

  6. POJ 1149 PIGS 建图,最大流

    题意: 你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开某一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 已打开的 ...

  7. POJ 1149 PIGS (AC这道题很不容易啊)网络流

    PIGS Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlo ...

  8. POJ 1149 PIGS 【网络流】

    题意: m n   //有m个猪圈,n个人卖猪. a1...am    //编号为i的猪圈里有ai头猪. b1 c1...cb1 d1   //第i个人有bi把钥匙,分别是ci猪圈的,其它猪圈里的猪都 ...

  9. POJ 1149 PIGS ★(经典网络流构图)

    [题意] 有M个猪圈,每个猪圈里初始时有若干头猪.一开始所有猪圈都是关闭的.依 次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪.每 个顾客分别都有他能够买的数量的上限.每个顾客走后, ...

随机推荐

  1. DataTable用法

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...

  2. Linux编译安装MySQL5.6

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4311061.html ...

  3. noi1816 画家问题(技巧搜索Dfs)

    /* Problem 画家问题 假设一个ans数组存的是对每一个点的操作 0表示不图 1表示图 那么 对于原图 g 操作第三行时对第一行没有影响 同样往下类似的 所以 假设我们知道了ans的第一行就是 ...

  4. JAVA学习笔记--二

    一.抽象类: 访问修饰符 abstract class 类名{ } 抽象类和普通类的区别: 1. 抽象类不能被实例化 2. 抽象类一般含有抽象方法 抽象方法:在抽象类中只有方法签名(方法声明),没有方 ...

  5. 关于百度 UEditor的使用

    1.文件路径的配置: 注意:在页面上需要指定editor文件所在的路径,否则报错 后面有时间,再说说 kindEditor和  bootstrap3的summernote的  Editor,  fck ...

  6. cmd运行java程序,无黑框闪烁

    程序目录中创建 “启动.bat” @echo off set mypath="%~dp0myjar.jar" echo %mypath% start javaw -jar %myp ...

  7. Linux 自动更新时间

    1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...

  8. 向RichTextBox控件不停的AppendText数据时,如何把光标的焦点始终显示到最后

    上面是csdn上的一个网友的问题,我的一个实现如下://让文本框获取焦点this.richTextBoxInfo.Focus();//设置光标的位置到文本尾this.richTextBoxInfo.S ...

  9. Vijos1865 NOI2014 魔法森林 LCT维护生成树

    基本思路: 首先按照weightA升序排序,然后依次在图中加边,并维护起点到终点路径上weightB的最大值 如果加边过程中生成了环,则删除环中weightB最大的边 由于是无向图,点之间没有拓扑序, ...

  10. JDK和JRE的区别?

    很多朋友可能跟我一样,已经使用JAVA开发很久了,可是对JDK,JRE,JVM这三者的联系与区别,一直都是模模糊糊的. 今天特写此文,来整理下三者的关系. JDK : Java Development ...