【BZOJ 1305】[CQOI2009]dance跳舞
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
男生和女生每个人都分身成两个节点
即x[1],x[2]和y[1],y[2]
然后如果i和j不相互喜欢
那么add(x[i][2],y[j][2],1)
如果相互喜欢的话
add(x[i][1],y[j][1],1)
然后对于每个男生
add(x[i][1],y[i][1],k)
对于每个女生
add(y[i][2],y[i][1],k)
然后对于每个男生
add(s,x[i][1],mid)
add(y[i][1],t,mid)
这里的mid是二分的值。
这个mid就是舞会的轮数了。
如果能够满流显然每个人都能配对跳mid支舞
显然是有单调性的。
(然后发现原来的dicnic的模板是错的。。边数的计算要特别careful.
【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 50;
struct abc{
int nex,en,flow;
}bian[2][N*N*2+8*N+10];
int n,k,x[N+10][3],y[N+10][3],fir[2][N*4+10],tfir[N*4+10],totm,deep[N*4+10];
int cnt = 0;
bool bo[N+10][N+10];
char s[N+10];
queue<int> dl;
//每个男人分为(x1,x2)
//每个女人分为(y1,y2)
void add(int x,int y,int cost,int i){
bian[i][totm].nex = fir[i][x];
fir[i][x] = totm;
bian[i][totm].en = y,bian[i][totm].flow = cost;
totm++;
bian[i][totm].nex = fir[i][y];
fir[i][y] = totm;
bian[i][totm].en = x,bian[i][totm].flow = 0;
totm++;
}
bool bfs(int s,int t){
dl.push(s);
memset(deep,255,sizeof deep);
deep[s] = 0;
while (!dl.empty()){
int x = dl.front();
dl.pop();
for (int temp = fir[1][x]; temp!= -1 ;temp = bian[1][temp].nex){
int y = bian[1][temp].en;
if (deep[y]==-1 && bian[1][temp].flow>0){
deep[y] = deep[x] + 1;
dl.push(y);
}
}
}
return deep[t]!=-1;
}
int dfs(int x,int t,int limit){
if (x == t) return limit;
if (limit == 0) return 0;
int cur,f = 0;
for (int temp = tfir[x];temp!=-1;temp = bian[1][temp].nex){
tfir[x] = temp;
int y = bian[1][temp].en;
if (deep[y] == deep[x] + 1 && (cur = dfs(y,t,min(limit,bian[1][temp].flow))) ){
f += cur;
limit -= cur;
bian[1][temp].flow -= cur;
bian[1][temp^1].flow += cur;
if (!limit) break;
}
}
return f;
}
int get_flow(){
int now = 0;
while (bfs(0,cnt)){
for (int i = 0;i <= cnt;i++) tfir[i] = fir[1][i];
int xxx = dfs(0,cnt,10000);
now+=xxx;
}
return now;
}
int main(){
//freopen("F:\\program\\rush\\rush_in.txt","r",stdin);
ios::sync_with_stdio(0),cin.tie(0);
memset(fir[0],255,sizeof fir[0]);
cin >> n >> k;
for (int i = 1;i <= n;i++){
cin >> (s+1);
for (int j = 1;j <= n;j++)
if (s[j]=='Y'){
bo[i][j] = 1;
}
}
for (int i = 1;i <= n;i++){
for (int j = 1;j <= 2;j++)
x[i][j] = ++cnt;
}
for (int i = 1;i <= n;i++)
for (int j = 1;j <= 2;j++)
y[i][j] = ++cnt;
cnt++;
for (int i = 1;i <= n;i++){
for (int j = 1;j <= n;j++){
if (bo[i][j])
add(x[i][1],y[j][1],1,0);
else{
add(x[i][2],y[j][2],1,0);
}
}
}
for (int i = 1;i <= n;i++){
add(x[i][1],x[i][2],k,0);
add(y[i][2],y[i][1],k,0);
}
int l = 0,r = n+1,temp1 = 0;
while (l<=r){
int i = (l+r)>>1;
for (int j = 0;j < totm;j++) bian[1][j] = bian[0][j];
for (int j = 0;j <= cnt;j++) fir[1][j] = fir[0][j];
int tt = totm;
for (int j = 1;j <= n;j++){
add(0,x[j][1],i,1);
add(y[j][1],cnt,i,1);
}
int temp = get_flow();
if (temp!=i*n){
r = i-1;
}else {
temp1 = i;
l = i+1;
}
totm = tt;
}
cout<<temp1<<endl;
return 0;
}
【BZOJ 1305】[CQOI2009]dance跳舞的更多相关文章
- bzoj 1305: [CQOI2009]dance跳舞
题目链接 bzoj 1305: [CQOI2009]dance跳舞 题解 男,女生拆点A1A2,B1B2,拆成两点间分别连容量为K的边,限制与不喜欢的人跳舞的数量 A1连接源点容量为x,B1连接汇点容 ...
- BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流
1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
- BZOJ 1305: [CQOI2009]dance跳舞( 最大流 )
云神代码很短...0 ms过的...看了代码 , 大概是贪心... orz 我不会证 数据这么小乱搞就可以了吧... ←_← 这道题网络流还是可以写的... 既然限制了最多只能和 k 个不喜欢的人da ...
- BZOJ 1305 [CQOI2009]dance跳舞(二分+网络流)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1305 [题目大意] 一次舞会有n个男孩和n个女孩. 每首曲子开始时,所有男孩和女孩恰好 ...
- BZOJ 1305: [CQOI2009]dance跳舞 网络最大流_二分答案_建模
Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...
- BZOJ 1305 CQOI2009 dance跳舞 二分答案+最大流
题目大意:给定n个男生和n个女生,一些互相喜欢而一些不.举行几次舞会,每次舞会要配成n对.不能有同样的组合出现.每一个人仅仅能与不喜欢的人跳k次舞,求最多举行几次舞会 将一个人拆成两个点.点1向点2连 ...
- bzoj 1305: [CQOI2009]dance 二分+網絡流判定
1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1340 Solved: 581[Submit][Sta ...
- 1305: [CQOI2009]dance跳舞 - BZOJ
Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...
- 1305: [CQOI2009]dance跳舞
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4169 Solved: 1804[Submit][Status][Discuss] Descripti ...
- BZOJ 1305:dance跳舞(二分+最大流)
一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩 ...
随机推荐
- 性能测试之Jforum平台的搭建
学习Jmeter性能基础,想要借助1款现有的软件平台,来练习jmeter基础,<Jmeter实战>书籍上给出样例软件平台:Jforum 一.环境准备 准备:tomcat9.mysql5.5 ...
- 关于错误CSC : error CS0006:未能找到元数据文件
在不同的解决方案中把一个项目搬来搬去,终于出现了传说的CSC : error CS0006.编译的时候总是提示一个引用中不存在的项找不到.无论怎样删除项目,删除引用都没法通过生成. 最终解决方案: 用 ...
- 警告: The APR based Apache Tomcat Native library failed to load.
警告: The APR based Apache Tomcat Native library failed to load. The error reported was [C:\apache-tom ...
- ie6下position:fixed定位问题
1. *html{ background-image:url(about:blank); background-attachment:fixed;}2.将需要用固定定位的元素中加上_position: ...
- jqury+animation+setTimeOut实现渐变显示与隐藏动画
初始效果 实现效果 1,编写HTMl结构代码 <div class="box"> <i class="icon"></i> ...
- JDBC 具体解释(1)
JDBC 具体解释(1) 在以java application server应用为主的平台是,JDBC的最高级应用是DataSource的实现,其他的JDO,webcache,hibe ...
- 【iOS开发系列】XIB IBOutlets use strong or weak ?
有人问.在ARC下,IBOutlets究竟应该定义成strong 还是 weak ?支持这个答案的人最多.答案仅是摘自官方文档的一个片段: From a practical perspective, ...
- c#自己实现线程池功能(二)
介绍 在上一篇c#自己实现线程池功能(一)中,我们基本实现了一个能够执行的程序.而不能真正的称作线程池.因为是上篇中的代码有个致命的bug那就是没有任务是并非等待,而是疯狂的进行while循环,并试图 ...
- 英语发音规则---D字母
英语发音规则---D字母 一.总结 一句话总结: 1.D发[d]音? doctor ['dɒktə] n. 医生:博士 bread [bred] n. 面包:生计 hand [hænd] n. 手,手 ...
- Nosql的实际应用场景
怎么样把NoSQL引入到我们的系统架构设计中,需要根据我们系统的业务场景来分析,什么样类型的数据适合存储在NoSQL数据库中,什么样类型的数据必须使用关系数据库存储.明确引入的NoSQL数据库带给系统 ...