网络流入门题。

源点到每一个学生连一条边,容量为1

每个学校到汇点连一条边,容量为L

符合要求的学生和学校之间连边,容量为1。

从源点到汇点的最大流就是答案。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<vector>
  5. #include<map>
  6. #include<queue>
  7. #include<string>
  8. #include<iostream>
  9. #include<algorithm>
  10. using namespace std;
  11.  
  12. int tot;
  13. int A,B;
  14. const int maxn = ;
  15. const int INF = 0x7FFFFFFF;
  16. struct Edge
  17. {
  18. int from, to, cap, flow;
  19. Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
  20. };
  21. vector<Edge>edges;
  22. vector<int>G[maxn];
  23. bool vis[maxn];
  24. int d[maxn];
  25. int cur[maxn];
  26. int n, m, s, t;
  27. struct Stu
  28. {
  29. vector<int>school;
  30. vector<int>flag;
  31. }stu[];
  32. struct Sch
  33. {
  34. bool flag[+];
  35. }h[];
  36.  
  37. map<string,int>zhuan;
  38.  
  39. void init()
  40. {
  41. for (int i = ; i < maxn; i++) G[i].clear();
  42. edges.clear();
  43. s=;
  44. t=A+B+;
  45. tot=;
  46. zhuan.clear();
  47. }
  48.  
  49. void AddEdge(int from, int to, int cap)
  50. {
  51. edges.push_back(Edge(from, to, cap, ));
  52. edges.push_back(Edge(to, from, , ));
  53. int w = edges.size();
  54. G[from].push_back(w - );
  55. G[to].push_back(w - );
  56. }
  57. bool BFS()
  58. {
  59. memset(vis, , sizeof(vis));
  60. queue<int>Q;
  61. Q.push(s);
  62. d[s] = ;
  63. vis[s] = ;
  64. while (!Q.empty())
  65. {
  66. int x = Q.front();
  67. Q.pop();
  68. for (int i = ; i<G[x].size(); i++)
  69. {
  70. Edge e = edges[G[x][i]];
  71. if (!vis[e.to] && e.cap>e.flow)
  72. {
  73. vis[e.to] = ;
  74. d[e.to] = d[x] + ;
  75. Q.push(e.to);
  76. }
  77. }
  78. }
  79. return vis[t];
  80. }
  81. int DFS(int x, int a)
  82. {
  83. if (x == t || a == )
  84. return a;
  85. int flow = , f;
  86. for (int &i = cur[x]; i<G[x].size(); i++)
  87. {
  88. Edge e = edges[G[x][i]];
  89. if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
  90. {
  91. edges[G[x][i]].flow+=f;
  92. edges[G[x][i] ^ ].flow-=f;
  93. flow+=f;
  94. a-=f;
  95. if(a==) break;
  96. }
  97. }
  98. if(!flow) d[x] = -;
  99. return flow;
  100. }
  101. int dinic(int s, int t)
  102. {
  103. int flow = ;
  104. while (BFS())
  105. {
  106. memset(cur, , sizeof(cur));
  107. flow += DFS(s, INF);
  108. }
  109. return flow;
  110. }
  111.  
  112. int main()
  113. {
  114. while(~scanf("%d%d",&A,&B))
  115. {
  116. if(!A&&!B) break;
  117. init();
  118.  
  119. for(int i=;i<=A;i++) AddEdge(s,i,);
  120.  
  121. for(int i=;i<=A;i++)
  122. {
  123. int d,p; scanf("%d%d",&d,&p);
  124.  
  125. stu[i].flag.clear();
  126. stu[i].school.clear();
  127.  
  128. for(int j=;j<=d;j++)
  129. {
  130. string name; cin>>name;
  131. if(zhuan[name]==) {zhuan[name]=tot;tot++;}
  132. stu[i].flag.push_back(zhuan[name]);
  133. }
  134. for(int j=;j<=p;j++)
  135. {
  136. int x; scanf("%d",&x);
  137. stu[i].school.push_back(x);
  138. }
  139. }
  140.  
  141. for(int i=;i<=B;i++)
  142. {
  143. int L,F;scanf("%d%d",&L,&F);
  144. AddEdge(i+A,t,L);
  145. memset(h[i].flag,,sizeof h[i].flag);
  146. for(int j=;j<=F;j++)
  147. {
  148. string name; cin>>name;
  149. if(zhuan[name]==) {zhuan[name]=tot;tot++;}
  150. h[i].flag[zhuan[name]]=;
  151. }
  152. }
  153.  
  154. for(int i=;i<=A;i++)
  155. {
  156. for(int j=;j<stu[i].school.size();j++)
  157. {
  158. int xue=stu[i].school[j];
  159. for(int k=;k<stu[i].flag.size();k++)
  160. if(h[xue].flag[stu[i].flag[k]])
  161. AddEdge(i,A+xue,);
  162. }
  163. }
  164. printf("%d\n",dinic(s,t));
  165. }
  166.  
  167. return ;
  168. }

FZU 1397 保送的更多相关文章

  1. FZU 2137 奇异字符串 后缀树组+RMQ

    题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...

  2. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  3. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  4. FZU 2112 并查集、欧拉通路

    原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...

  5. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  6. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  7. ACM: FZU 2102 Solve equation - 手速题

     FZU 2102   Solve equation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  8. ACM: FZU 2110 Star - 数学几何 - 水题

     FZU 2110  Star Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u  Pr ...

  9. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

随机推荐

  1. 求n!末尾0的个数

    题目连接 /* £:离散数学. £:n!中2的个数>5的个数. £:2*5=10: */ #include<cstdio> #include<cstring> #incl ...

  2. MVVM的架构设计与团队协作 with StoryBoard

    今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...

  3. Android中Edittext的属性

    //此为转载别人的,挺不错的 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true"  ...

  4. Sea.Js使用入门

    1.Sea.Js是什么 seajs相对于RequireJs与LabJS就比较年轻,2010年玉伯发起了这个开源项目,SeaJS遵循CMD规范,与RequireJS类似,同样做为模块加载器.示例 // ...

  5. CodeForces 678C Joty and Chocolate

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  6. NYOJ 925 国王的烦恼

    从最后一天开始往前加边. 同一天的边同时加到图上,加完之后检查集合数量是否和没加之前有变化. 有变化的话,答案就+1. #include<cstdio> #include <iost ...

  7. MYSQL数据库的套接字文件,pid文件,表结构文件

    socket文件:当用Unix域套接字方式进行连接时需要的文件. pid文件:MySQL实例的进程ID文件. MySQL表结构文件:用来存放MySQL表结构定义文件. 套接字文件 Unix系统下本地连 ...

  8. MongoDB如何设置权限(类似关系型数据库的用户名和密码)

    MongoDB 缺省是没有设置鉴权的,业界大部分使用 MongoDB 的项目也没有设置访问权限.这就意味着只要知道 MongoDB 服务器的端口,任何能访问到这台服务器的人都可以查询和操作 Mongo ...

  9. 团队开发里频繁使用 git rebase 来保持树的整洁好吗?

    用了以后, 树可以非常清晰, 某种程度上便于追踪, 但是 push --force 就多多了,不用呢, 合并没有远程仓库被修改的麻烦, 可是追踪又不清晰... git rebase是对commit h ...

  10. 使用C语言扩展Python

    开发环境:Ubuntu9.10,python2.6,gcc4.4.1 1,ubuntu下的python运行包和开发包是分开的,因此需要在新利得里面安装python-all-dev,从而可以在代码中引用 ...