网络流入门题。

源点到每一个学生连一条边,容量为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 保送的更多相关文章

  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. Smoke Testing(冒烟测试)

    Smoke Testing 的概念最早源于制造业,用于测试管道.测试时,用鼓风机往管道里灌烟,看管壁外面是否有烟冒出来,以便检验管道是否有缝隙.这一测试显然比较初级,更深层一点的测试至少要进行渗油测试 ...

  2. 异步加载AsyncTask

    private void huodeshuju() {        new AsyncTask<String, Void, String>() {            @Overrid ...

  3. github入门操作

    一.更新github上的已有项目: 将repository clone到本地 shanyu@debian:~/Git$ git clone https://github.com/xunbu7/Hell ...

  4. AI 人工智能 探索 (十)

    呼叫事件图形结构如下 蓝色代表 警察局 红色代表警察 黄色代表 死亡人 蓝色球代表呼救人 黑色代表 敌人 警察目标是 攻击 黑色人,但 路中 会碰到 黄色人,如果警察有 救人功能 则会先救人去医院再看 ...

  5. js滚动条

    /*滚动条在Y轴上的滚动距离*/function ScrollTop(){ var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0; i ...

  6. java下载多个文件文件

    第一步 将要下载的多个文件打包成ZIP格式 public String makeZip(List<String> path) throws IOException{ byte[] buff ...

  7. php简单命令代码集锦

    if(file_exists("file.htm"))// 检查是否存在此文件 if(file_exists("chat"))//检查是否存在此文件夹 rena ...

  8. PAT1002

    This time, you are supposed to find A+B where A and B are two polynomials. 这一次,你被要求计算A+B当A和B是两个多项式的时 ...

  9. L5,no wrong numbers

    expressions: up to now,到现在为止 a great many,数量很大 in a way,在某种意义上说   words: burn,vt燃烧,vi烧毁,n灼烧 obtain,v ...

  10. MediaScanner与音乐信息扫描==

    http://www.eoeandroid.com/forum.php?mod=viewthread&tid=98713 =================================== ...