FZU 1397 保送
网络流入门题。
源点到每一个学生连一条边,容量为1
每个学校到汇点连一条边,容量为L
符合要求的学生和学校之间连边,容量为1。
从源点到汇点的最大流就是答案。
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<vector>
- #include<map>
- #include<queue>
- #include<string>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- int tot;
- int A,B;
- const int maxn = ;
- const int INF = 0x7FFFFFFF;
- struct Edge
- {
- int from, to, cap, flow;
- Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
- };
- vector<Edge>edges;
- vector<int>G[maxn];
- bool vis[maxn];
- int d[maxn];
- int cur[maxn];
- int n, m, s, t;
- struct Stu
- {
- vector<int>school;
- vector<int>flag;
- }stu[];
- struct Sch
- {
- bool flag[+];
- }h[];
- map<string,int>zhuan;
- void init()
- {
- for (int i = ; i < maxn; i++) G[i].clear();
- edges.clear();
- s=;
- t=A+B+;
- tot=;
- zhuan.clear();
- }
- void AddEdge(int from, int to, int cap)
- {
- edges.push_back(Edge(from, to, cap, ));
- edges.push_back(Edge(to, from, , ));
- int w = edges.size();
- G[from].push_back(w - );
- G[to].push_back(w - );
- }
- bool BFS()
- {
- memset(vis, , sizeof(vis));
- queue<int>Q;
- Q.push(s);
- d[s] = ;
- vis[s] = ;
- while (!Q.empty())
- {
- int x = Q.front();
- Q.pop();
- for (int i = ; i<G[x].size(); i++)
- {
- Edge e = edges[G[x][i]];
- if (!vis[e.to] && e.cap>e.flow)
- {
- vis[e.to] = ;
- d[e.to] = d[x] + ;
- Q.push(e.to);
- }
- }
- }
- return vis[t];
- }
- int DFS(int x, int a)
- {
- if (x == t || a == )
- return a;
- int flow = , f;
- for (int &i = cur[x]; i<G[x].size(); i++)
- {
- Edge e = edges[G[x][i]];
- if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
- {
- edges[G[x][i]].flow+=f;
- edges[G[x][i] ^ ].flow-=f;
- flow+=f;
- a-=f;
- if(a==) break;
- }
- }
- if(!flow) d[x] = -;
- return flow;
- }
- int dinic(int s, int t)
- {
- int flow = ;
- while (BFS())
- {
- memset(cur, , sizeof(cur));
- flow += DFS(s, INF);
- }
- return flow;
- }
- int main()
- {
- while(~scanf("%d%d",&A,&B))
- {
- if(!A&&!B) break;
- init();
- for(int i=;i<=A;i++) AddEdge(s,i,);
- for(int i=;i<=A;i++)
- {
- int d,p; scanf("%d%d",&d,&p);
- stu[i].flag.clear();
- stu[i].school.clear();
- for(int j=;j<=d;j++)
- {
- string name; cin>>name;
- if(zhuan[name]==) {zhuan[name]=tot;tot++;}
- stu[i].flag.push_back(zhuan[name]);
- }
- for(int j=;j<=p;j++)
- {
- int x; scanf("%d",&x);
- stu[i].school.push_back(x);
- }
- }
- for(int i=;i<=B;i++)
- {
- int L,F;scanf("%d%d",&L,&F);
- AddEdge(i+A,t,L);
- memset(h[i].flag,,sizeof h[i].flag);
- for(int j=;j<=F;j++)
- {
- string name; cin>>name;
- if(zhuan[name]==) {zhuan[name]=tot;tot++;}
- h[i].flag[zhuan[name]]=;
- }
- }
- for(int i=;i<=A;i++)
- {
- for(int j=;j<stu[i].school.size();j++)
- {
- int xue=stu[i].school[j];
- for(int k=;k<stu[i].flag.size();k++)
- if(h[xue].flag[stu[i].flag[k]])
- AddEdge(i,A+xue,);
- }
- }
- printf("%d\n",dinic(s,t));
- }
- return ;
- }
FZU 1397 保送的更多相关文章
- FZU 2137 奇异字符串 后缀树组+RMQ
题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- FZU 2112 并查集、欧拉通路
原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...
- ACM: FZU 2107 Hua Rong Dao - DFS - 暴力
FZU 2107 Hua Rong Dao Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- ACM: FZU 2102 Solve equation - 手速题
FZU 2102 Solve equation Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- ACM: FZU 2110 Star - 数学几何 - 水题
FZU 2110 Star Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Pr ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
随机推荐
- 求n!末尾0的个数
题目连接 /* £:离散数学. £:n!中2的个数>5的个数. £:2*5=10: */ #include<cstdio> #include<cstring> #incl ...
- MVVM的架构设计与团队协作 with StoryBoard
今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- Android中Edittext的属性
//此为转载别人的,挺不错的 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" ...
- Sea.Js使用入门
1.Sea.Js是什么 seajs相对于RequireJs与LabJS就比较年轻,2010年玉伯发起了这个开源项目,SeaJS遵循CMD规范,与RequireJS类似,同样做为模块加载器.示例 // ...
- CodeForces 678C Joty and Chocolate
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- NYOJ 925 国王的烦恼
从最后一天开始往前加边. 同一天的边同时加到图上,加完之后检查集合数量是否和没加之前有变化. 有变化的话,答案就+1. #include<cstdio> #include <iost ...
- MYSQL数据库的套接字文件,pid文件,表结构文件
socket文件:当用Unix域套接字方式进行连接时需要的文件. pid文件:MySQL实例的进程ID文件. MySQL表结构文件:用来存放MySQL表结构定义文件. 套接字文件 Unix系统下本地连 ...
- MongoDB如何设置权限(类似关系型数据库的用户名和密码)
MongoDB 缺省是没有设置鉴权的,业界大部分使用 MongoDB 的项目也没有设置访问权限.这就意味着只要知道 MongoDB 服务器的端口,任何能访问到这台服务器的人都可以查询和操作 Mongo ...
- 团队开发里频繁使用 git rebase 来保持树的整洁好吗?
用了以后, 树可以非常清晰, 某种程度上便于追踪, 但是 push --force 就多多了,不用呢, 合并没有远程仓库被修改的麻烦, 可是追踪又不清晰... git rebase是对commit h ...
- 使用C语言扩展Python
开发环境:Ubuntu9.10,python2.6,gcc4.4.1 1,ubuntu下的python运行包和开发包是分开的,因此需要在新利得里面安装python-all-dev,从而可以在代码中引用 ...