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

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

  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
  2.  
  3. 网络流问题的难点还是在于建图
  4.  
  5. 题意:一个工人在养猪场工作,他负责卖猪,现在有m个猪圈和n个顾客,每个猪圈的容量没有限制,但是工人没有猪圈的钥匙,而前来的顾客有这些猪圈中一些猪圈的钥匙,每个顾客来之后会打开所有他有钥匙的猪圈,然后买完自己想要买的猪的个数后,关闭所有他打开的猪圈,顾客总是一个接一个的来,当猪圈打开的时候,工人可以随意挪动打开的猪圈中的猪(只能在打开的猪圈中相互移动)问工人一天最多卖出去多少猪
  6.  
  7. 输入:开头第一行两个整数mn分别代表猪圈数和顾客数,接下来一行m个数分别表示对应的猪圈中猪的个数,在接下来n行每行第一个数a,表示接下来a个数意思是当前行(第i行)i顾客所持的猪圈的钥匙,最后一个数b代表这个顾客要买b头猪
  8.  
  9. 题解:1、超级源点连接第一个到达j猪圈的顾客 权值为第j个猪圈的猪的头数,
    2、因为每个同时打开的猪圈之间的猪可以相互走动,所以所有连接到j猪圈的顾客相互连接容量为无穷大
    3、处理重边,比如说第一个猪圈的第一个顾客和第二个猪圈的第一个顾客都是cus1,第三个猪圈的第一个顾客是cus2,所以就有源点到cus1的重边,此时将这两条边合并即可,流量为二者流量和
    4、每一个顾客连接超级汇点
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. #include<queue>
  5. #include<algorithm>
  6. #include<vector>
  7. #define MAXM 100100
  8. #define MAX 10010
  9. #define INF 0x7ffff
  10. using namespace std;
  11. vector<int>map[MAXM];
  12. int n,m,a,b[MAX];
  13. int ans,head[MAX];
  14. int cur[MAX],dis[MAX];
  15. int vis[MAX];
  16. int pig[MAX];
  17. struct node
  18. {
  19. int from,to,cap,flow,next;
  20. }edge[MAXM];
  21. void init()
  22. {
  23. ans=0;
  24. memset(head,-1,sizeof(head));
  25. for(int i=1;i<=m;i++)
  26. map[i].clear();
  27. }
  28. void add(int u,int v,int w)
  29. {
  30. int i,j;
  31. for(i=head[u];i!=-1;i=edge[i].next)
  32. {
  33. if(edge[i].to==v) //判断是否有重边
  34. break;
  35. }
  36. if(i==-1)//i==-1表示当前这个点并没有边与其相连
  37. {
  38. edge[ans]={u,v,w,0,head[u]};
  39. head[u]=ans++;
  40. edge[ans]={v,u,0,0,head[v]};
  41. head[v]=ans++;
  42. }
  43. else//如果有重边 则将流量合并
  44. if(w!=INF)
  45. edge[i].cap+=w;
  46. }
  47. void input()
  48. {
  49. int i,j,key;
  50. for(i=1;i<=m;i++)
  51. scanf("%d",&pig[i]);
  52. for(i=1;i<=n;i++)
  53. {
  54. scanf("%d",&a);
  55. while(a--)
  56. {
  57. scanf("%d",&key);
  58. map[key].push_back(i);//存储先后到第可以个猪圈的顾客编号
  59. }
  60. scanf("%d",&b[i]);
  61. }
  62. }
  63. void getmap()
  64. {
  65. int i,j;
  66. for(i=1;i<=m;i++)
  67. {
  68. int u=map[i][0];
  69. add(0,u,pig[i]);//超级源点连接第一个到i猪圈的顾客
  70. for(j=0;j<map[i].size()-1;j++)
  71. add(map[i][j],map[i][j+1],INF);//所有到i猪圈的顾客按照顺序连接
  72. }
  73. for(i=1;i<=n;i++)
  74. add(i,n+1,b[i]);//所有顾客连接汇点
  75. }
  76. int bfs(int beg,int end)
  77. {
  78. int i;
  79. memset(vis,0,sizeof(vis));
  80. memset(dis,-1,sizeof(dis));
  81. queue<int>q;
  82. while(!q.empty())
  83. q.pop();
  84. vis[beg]=1;
  85. dis[beg]=0;
  86. q.push(beg);
  87. while(!q.empty())
  88. {
  89. int u=q.front();
  90. q.pop();
  91. for(i=head[u];i!=-1;i=edge[i].next)//遍历所有的与u相连的边
  92. {
  93. node E=edge[i];
  94. if(!vis[E.to]&&E.cap>E.flow)//如果边未被访问且流量未满继续操作
  95. {
  96. dis[E.to]=dis[u]+1;//建立层次图
  97. vis[E.to]=1;//将当前点标记
  98. if(E.to==end)//如果当前点搜索到终点则停止搜索 返回1表示有从原点到达汇点的路径
  99. return 1;
  100. q.push(E.to);//将当前点入队
  101. }
  102. }
  103. }
  104. return 0;//返回0表示未找到从源点到汇点的路径
  105. }
  106. int dfs(int x,int a,int end)//把找到的这条边上的所有当前流量加上a(a是这条路径中的最小残余流量)
  107. {
  108. //int i;
  109. if(x==end||a==0)//如果搜索到终点或者最小的残余流量为0
  110. return a;
  111. int flow=0,f;
  112. for(int& i=cur[x];i!=-1;i=edge[i].next)//i从上次结束时的弧开始
  113. {
  114. node& E=edge[i];
  115. if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)//如果
  116. {//bfs中我们已经建立过层次图,现在如果 dis[E.to]==dis[x]+1表示是我们找到的路径
  117. //如果dfs>0表明最小的残余流量还有,我们要一直找到最小残余流量为0
  118. E.flow+=f;//正向边当前流量加上最小的残余流量
  119. edge[i^1].flow-=f;//反向边
  120. flow+=f;//总流量加上f
  121. a-=f;//最小可增流量减去f
  122. if(a==0)
  123. break;
  124. }
  125. }
  126. return flow;//所有边加上最小残余流量后的值
  127. }
  128. int Maxflow(int beg,int end)
  129. {
  130. int flow=0;
  131. while(bfs(beg,end))//存在最短路径
  132. {
  133. memcpy(cur,head,sizeof(head));//复制数组
  134. flow+=dfs(beg,INF,end);
  135. }
  136. return flow;//最大流量
  137. }
  138. int main()
  139. {
  140. while(scanf("%d%d",&m,&n)!=EOF)
  141. {
  142. init();
  143. input();
  144. getmap();
  145. printf("%d\n",Maxflow(0,n+1));
  146. }
  147. return 0;
  148. }

  

poj 1149 PIGS【最大流经典建图】的更多相关文章

  1. [poj] 1149 PIGS || 最大流经典题目

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

  2. poj--1149--PIGS(最大流经典建图)

    PIGS Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status D ...

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

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

  4. POJ1149 最大流经典建图PIG

    题意:       有一个人,他有m个猪圈,每个猪圈里都有一定数量的猪,但是他没有钥匙,然后依次来了n个顾客,每个顾客都有一些钥匙,还有他要卖猪的数量,每个顾客来的时候主人用顾客的钥匙打开相应的门,可 ...

  5. poj 1149 pigs ---- 最大流

    题意以及分析:http://ycool.com/post/zhhrrm6#rule3 主要是建图,简化图,然后在套最大流的模板. #include <iostream> #include& ...

  6. POJ 2226 最小点覆盖(经典建图)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8881   Accepted: 3300 Desc ...

  7. poj 1149 pigs(最大流)

    题目大意:迈克在农场工作,农场有 m 个猪舍,每个猪舍有若干只猪,但是迈克不能打开任何一间猪舍.有 n 个顾客前来购买,每个顾客有最大的购买数量,每个顾客可以购买某些猪舍的猪,且顾客可以打开这些猪舍, ...

  8. hdoj--5093--Battle ships(二分图经典建图)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

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

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

随机推荐

  1. Xcode 7.3.1的模拟器路径

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Core ...

  2. Shell编程练习

    1.使用case语句 2.使用while....do....done语句 3.使用

  3. (重)POJ 3020Antenna Placement

    http://poj.org/problem?id=3020 呃...这个题不是很会,所以找了大神的博客做了参考,说得很详细 http://blog.csdn.net/lyy289065406/art ...

  4. UVA 10801 Lift Hopping

    算是一道需要动脑筋的最短路问题了,关键在于建图部分,对于n个电梯中每一个都要经过cnt个楼层,a[0],a[1],a[2],a[3],a[4],......a[cnt-1],那么对于任意两个楼层a[j ...

  5. 优化 Android 线程和后台任务开发

    在 Android 开发中,你不应该做任何阻碍主线程的事情.但这究竟意味着什么呢?在这次海湾 Android 开发者大会讲座中,Ari Lacenski 认为对于长时间运行或潜在的复杂任务要特别小心. ...

  6. C语言动态内存管理

    1-概述 动态存储管理的基本问题是:系统如何按请求分配内存,如何回收内存再利用.提出请求的用户可能是系统的一个作业,也可能是程序中的一个变量. 空闲块 未曾分配的地址连续的内存区称为“空闲块”. 占用 ...

  7. 【DataStructure In Python】Python模拟链表

    最近一直在学习Python和Perl这两门语言,两者共同点很多,也有不多.希望通过这样的模拟练习可以让自己更熟悉语言,虽然很多时候觉得这样用Python或者Perl并没有体现这两者的真正价值. #! ...

  8. 函数 xdes_get_state

    得到XDES Entry中状态 /**********************************************************************//** Gets the ...

  9. C#的同步和异步调用方法

    同步和异步大家都明白什么意思,在这里不多介绍了. namespace ConsoleTest { class Program { static void Main(string[] args) { C ...

  10. BZOJ_1007_ [HNOI2008]_水平可见直线_(单调栈+凸包)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1007 给出一些直线,沿着y轴从上往下看,能看到多少条直线. 分析 由于直线相交,会遮挡住一些直 ...