HDU4560 二分最大流
http://acm.hdu.edu.cn/showproblem.php?pid=4560
网络流好像经常搭配上二分和拆点。
n个歌手,m种歌曲流派(n<=m<=75)
我们想要安排尽可能多的演唱会。不过有以下条件——
1,每场演唱会中,每个歌手要唱不同类型的歌曲。
2,这样可能导致有些歌手去唱他不擅长的歌曲。对于任一种歌曲,被不合适唱的次数都不能超过L。
问你最多能安排多少场演唱会
想了一想发现最大流不能直接流,因为很难表示一场比赛需要M个流派的条件
发现这道题总共有三个信息,歌手,流派和比赛场次。
我们考虑二分他的比赛场次,对于场次x
源点流向每一个歌手x,x流向他不擅长的歌曲1,或者流向他擅长的歌曲1,然后不擅长的歌曲流向同一首歌的擅长的歌曲K表示最多有K个歌手可以不擅长,最后所有擅长的歌曲流向源点t,x的大小。
这需要将每一首歌曲拆成擅长的和不擅长的两个点进行建图。
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
inline int read(){int now=;register char c=getchar();for(;!isdigit(c);c=getchar());
for(;isdigit(c);now=now*+c-'',c=getchar());return now;}
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
const double eps = 1e-;
const int maxn = 2e5 + ;
const int maxm = 6e5 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
struct Dinic{
struct Edge{
int from,to,cap,flow,nxt;
Edge() {}
Edge(int u,int v,int c,int f,int n):from(u),to(v),cap(c),flow(f),nxt(n) {}
}edge[maxm];
int n,s,t,E,head[maxn];
bool vis[maxn];
int d[maxn],cur[maxn];
inline void AddEdge(int f,int t,int c){
edge[++E] = Edge(f,t,c,,head[f]);
head[f] = E;
edge[++E] = Edge(t,f,,,head[t]);
head[t] = E;
}
inline void Init(int n,int s,int t){
this -> n = n; E = -;
this -> s = s; head[s] = -;
this -> t = t; head[t] = -;
for(int i = ; i <= n ; i ++) head[i] = -;
} inline bool BFS(){
memset(vis,,sizeof(vis));
queue<int>Q;
d[s] = ;vis[s] = ;
for(Q.push(s);!Q.empty();){
int x = Q.front(); Q.pop();
for(int nxt,i = head[x];~i;i = nxt){
Edge &e = edge[i]; nxt = e.nxt;
if(vis[e.to] || e.cap <= e.flow) continue;
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
return vis[t];
} inline int DFS(const int &x,int a){
if(x == t || a == ) return a;
int flow = ,f,nxt;
for(int &i = cur[x]; ~i; i = nxt){
Edge &e = edge[i]; nxt = e.nxt;
if(d[x] + != d[e.to]) continue;
f = DFS(e.to,min(a,e.cap - e.flow));
if(f <= ) continue;
e.flow += f;
edge[i ^ ].flow -= f;
flow += f; a -= f;
if(!a) break;
}
return flow;
}
inline int maxFlow(){return maxFlow(s,t);}
inline int maxFlow(int s,int t){
int flow = ;
for(;BFS();){
for(int i = ;i <= n ; i ++) cur[i] = head[i];
flow += DFS(s,INF);
}
return flow;
}
}g;
int N,M,L,K;
bool MAP[][];
bool check(int x){
int s = ,t = N + M * + ;
g.Init(t,s,t);
For(i,,N) g.AddEdge(s,i,x);
For(i,,N){
For(j,,M){
if(MAP[i][j]){
g.AddEdge(i,j + N,);
}else{
g.AddEdge(i,j + N + M,);
}
}
}
For(i,,M) g.AddEdge(i + N,t,x);
For(i,,M) g.AddEdge(i + N + M,i + N,K);
return ((LL)g.maxFlow() >= ((LL)x * N));
}
int solve(){
int l = ,r = INF;
int ans = ;
while(l <= r){
int m = (l + r) >> ;
if(check(m)){
ans = m;
l = m + ;
}else{
r = m - ;
}
}
return ans;
}
int main()
{
int T; Sca(T);
int CASE = ;
while(T--){
Sca2(N,M); Sca2(L,K); Mem(MAP,);
For(i,,L){
int x,y; Sca2(x,y);
MAP[x][y] = ;
}
printf("Case %d: ",CASE++);
Pri(solve());
}
#ifdef VSCode
system("pause");
#endif
return ;
}
HDU4560 二分最大流的更多相关文章
- hdu4560 不错的建图,二分最大流
题意: 我是歌手 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Subm ...
- uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流
二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...
- POJ3228二分最大流
题意: 有n个点,每个点有两个权值,金子数量还有仓库容量,金子可以存在自己的仓库里或者是别的仓库里,仓库和仓库之间有距离,问所有金子都必须存到库里最大距离的最小是多少? 思路: ...
- BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流
1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
- loj 1167(二分+最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26881 思路:我们可以二分最大危险度,然后建图,由于每个休息点只能 ...
- BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图
这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...
随机推荐
- 如何使用CSS 让Table的最后一列的右边框不显示
table{ border-collapse:collapse; } .templateColumn{ border-right:1px solid #AAA; } table.templateCon ...
- css 媒体查询 注意点
1, 媒体查询表达式之间还可以用逗号,@media (max-width:800px), print 它表示或的意思 @media (max-width: 800px) OR print; 2, n ...
- MySQL的my.cnf文件(解决5.7.18下没有my-default.cnf)
官网说:从5.7.18开始不在二进制包中提供my-default.cnf文件.参考:https://dev.mysql.com/doc/refman/5.7/en/binary-installatio ...
- Longest Ordered Subsequence POJ - 2533 最长上升子序列dp
题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- 洛谷P1330封锁阳光大学题解
题意 此题可以说是一个很裸的一个二分图染色,但是比较不同的是,这个图中可能是不联通的,因此我们需要找到所有的联通块,然后一一选出每个联通块中黑块与白块中最小的个数,然后加入到最后的答案中去,也是很坑的 ...
- 【XSY1905】【XSY2761】新访问计划 二分 树型DP
题目描述 给你一棵树,你要从\(1\)号点出发,经过这棵树的每条边至少一次,最后回到\(1\)号点,经过一条边要花费\(w_i\)的时间. 你还可以乘车,从一个点取另一个点,需要花费\(c\)的时间. ...
- 「2017 Multi-University Training Contest 1」2017多校训练1
1001 Add More Zero(签到题) 题目链接 HDU6033 Add More Zero 找出最大的k,使得\(2^m-1\ge 10^k\). 直接取log,-1可以忽略不计. #inc ...
- iptables(4)规则编写
/etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Tue Mar 20 15:05:33 2018*filter:INPUT ...
- windows7安装docker
因为本机已经安装了git,所以这里取消勾选 配置环境变量 进入到D:\DockerToolbox 将D:\DockerToolbox下的boot2docker.iso 复制到C:\Users\my\. ...
- Codeforces Round #508 (Div. 2) C D
C: C - Gambling 给你两个数列 每一回合A可以选择从第一个序列里面选一个数或者清除第二个序列里面选一个数 同理B能从第二序列里面选数或者清除第一个序列里面一个数 然后 求A所选的数之和 ...