网络流入门题。

源点到每一个学生连一条边,容量为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. zend frameword 基本语法

    #resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"#resources.frontCo ...

  2. 观光公交noip<贪心>

    题目链接:https://www.oj.swust.edu.cn/problem/show/1190 思路: 每在一段路上使用一次加速器,就会对某些人或者说某些路段上的人产生影响,目的是使产生的影响最 ...

  3. Debian 安装 vmware-tools 手记

    debian 8.5 源 deb http://ftp.de.debian.org/debian jessie main http://mirrors.163.com/.help/debian.htm ...

  4. 【Qt开发】修改源码文件的编码格式的小技巧 .

    默认情况下,代码文件应该以utf-8的格式来存储的.而如果在代码文件的转移或者上传下载过程中,弄乱了文件的编码格式,一般会出现乱码的情况. 例如windows系统下,中文就很容易出现乱码,如下图,文件 ...

  5. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  6. 2333: [SCOI2011]棘手的操作[写不出来]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...

  7. PHP开源CRM-推荐几个

    http://www.xinyou88.com/about/xcrm.html 因为医院要同步用户到诊统计的信息.是拿着表单来回送.途中大概有20分钟左右.有些机器和互联网可以搞定的事情.人力来做.在 ...

  8. C#中String和stringBuilder的区别

    Stringbuilder类是直接用于字符串操作的类,打个比方把(1)string aa="123456";(2)aa+="789"; (3)StringBui ...

  9. cakephp 的事件系统(Getting to grips with CakePHP’s events system), 基于观察者模式

    This article was written about CakePHP 2.x and has been untested with CakePHP 3.x CakePHP seems to g ...

  10. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...