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 ...
随机推荐
- boost库之geometry<二>
#include <boost/assign.hpp> #include <boost/geometry/core/point_type.hpp> #include <b ...
- Hbase shell 中能否通过filter实现的高级查询
import org.apache.hadoop.hbase.filter.CompareFilter import org.apache.hadoop.hbase.filter.SingleColu ...
- JS 的NULL undefined 空
null,对象不存在 var ii= document.getElementById("id"); alert(ii); 当前页面不存在id对象 undefined var i; ...
- 7--OC中NSLog函数输出格式详解
OC中NSLog函数输出格式详解 • %@ 对象 • %d, %i 整数 • %u 无符整形 • %f 浮点/双字 • %x, %X 二进制整数 • %o 八进制整数 • %zu size_t • % ...
- angularjs中动态为audio绑定src
今天在angularjs中用audio的时候碰到的这些问题,查阅http://www.cnblogs.com/rachelanlan/p/3598070.html获得解决,感谢! <div cl ...
- jq中的移除事件
unbind([type],[data]); 第一个参数是事件类型,第二个参数是将要移出的函数 说明: 1,如果没有参数,则删除所有绑定的事件 2,如果只提供了事件类型作为参数,则只删除该类型的绑定事 ...
- JQuery中一个简单的表单验证的实例
html代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 转:移植SlidingMenu Android library,和安装example出现的问题解决
很多项目都用到类似左侧滑动菜单的效果,比如facebook,evernote,VLC for android等等,这很酷 源代码可以从GitHub的https://github.com/jfeinst ...
- Beautiful Subarrays
Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- (转)多个mapreduce工作相互依赖处理方法完整实例(JobControl)
多个mapreduce工作相互依赖处理方法完整实例(JobControl) 原文地址:http://mntms.iteye.com/blog/2096456?utm_source=tuicool&am ...