TOJ 1705 Dining 最大流

深碍√TFBOYSˉ_ 2022-05-27 04:48 212阅读 0赞

描述

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

输入

Line 1: Three space-separated integers: N, F, and D
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

输出

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

20180411210157616

提示

One way to satisfy three cows is:
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

这道题建图需要思考一下的,我以我神乎其技的画技给大家画了这个模拟图….

大概就是设置一个S,T,然后要2个牛,左边的牛连吃的,右边的牛连喝的,然后让两头牛连一下。

20180411210321958

注意坐标哦,N最大100,所以每次都要+100哦。

然后就用最大流求一下就好啦~~

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<queue>
  9. #include<vector>
  10. using namespace std;
  11. const int inf=0x7f7f7f7f;
  12. int n,m,k,G[505][505],flow[505],pre[505];
  13. queue<int> q;
  14. int bfs(int s,int t)
  15. {
  16. while(!q.empty())
  17. q.pop();
  18. memset(pre,-1,sizeof pre);
  19. pre[s]=0,flow[s]=inf;
  20. q.push(s);
  21. while(!q.empty())
  22. {
  23. int p=q.front();
  24. q.pop();
  25. if(p==t)break;
  26. for(int u=1;u<=n;u++)
  27. {
  28. if(u!=s&&G[p][u]>0&&pre[u]==-1)
  29. {
  30. pre[u]=p;
  31. flow[u]=min(flow[p],G[p][u]);
  32. q.push(u);
  33. }
  34. }
  35. }
  36. if(pre[t]==-1)return -1;
  37. return flow[t];
  38. }
  39. int EK(int s,int t)
  40. {
  41. int delta=0,tot=0;
  42. while(1)
  43. {
  44. delta=bfs(s,t);
  45. if(delta==-1)break;
  46. int p=t;
  47. while(p!=s)
  48. {
  49. G[pre[p]][p]-=delta;
  50. G[p][pre[p]]+=delta;
  51. p=pre[p];
  52. }
  53. tot+=delta;
  54. }
  55. return tot;
  56. }
  57. int main()
  58. {
  59. int i,j,l,t,tt,x;
  60. while(scanf("%d%d%d",&n,&m,&k)!=EOF)
  61. {
  62. memset(G,0,sizeof G);
  63. memset(flow,0,sizeof flow);
  64. //0为超级源点,401为超级汇点
  65. for(i=1;i<=m;i++)
  66. G[0][i]=1;
  67. for(i=1;i<=k;i++)
  68. G[i+300][401]=1;
  69. for(i=1;i<=n;i++)
  70. G[i+100][i+200]=1;
  71. for(i=1;i<=n;i++)
  72. {
  73. scanf("%d%d",&t,&tt);
  74. for(j=0;j<t;j++)
  75. {
  76. scanf("%d",&x);
  77. G[x][i+100]=1;
  78. }
  79. for(l=0;l<tt;l++)
  80. {
  81. scanf("%d",&x);
  82. G[i+200][x+300]=1;
  83. }
  84. }
  85. n=401;
  86. printf("%d\n",EK(0,401));
  87. }
  88. }

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读

    相关 TOJ 3711 浪漫自习

    如今的校园谈恋爱已是习以为常,这不,去上自习也要成双成对的。现在假设某班里有N对情侣从同一寝室楼出发,到达同一个教室上自习。途中,她们可能会经过长廊、静溪等一系列的景点观光游览