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 ...
随机推荐
- Sharepoint 2016 - Deploy Office Online Server
Step 1: Install prerequisite software for Office Online Server To install Office Online Server Ope ...
- SQL to JSON Data Modeling with Hackolade
Review: SQL to JSON data modeling First, let’s review, the main way to represent relations in a rela ...
- Servlet篇 之 web服务器
创建web项目,在web项目中创建html页面,然后把项目部署到web服务器里面,启动服务器之后,可以使用浏览器通过URL地址的方式,访问到web项目中的html页面了 Web服务器: 常用tomca ...
- Nginx stream ssl
L 115 端口监听 netstat -anp | (端口名)
- 因为网络安全的重要性打算学习linux
互联网的普及,在给人们带来巨大便利的同时,也让人们感受到网络安全隐患带给人们的不安与威胁.尤其是随着计算机技术和网络技术应用范围的不断扩充,网络安全方面存在的漏洞也越来越多,在这种情况下,如何提高网络 ...
- python打印log重复问题
本博客转载于:http://www.cnblogs.com/huang-yc/p/9209096.html,写得真不错 浅析python日志重复输出问题 目录 问题起源: 问题解析 解决办法 1.改名 ...
- C#常用的命名规则汇总
C#常用的命名规则汇总 来源 https://www.cnblogs.com/pengyouqiang88/p/5021128.html 本文转载自脚本之家 本文详细汇总了C#常用的命名规则.分享给大 ...
- Windows server install mrtg
由于MRTG使用Perl语言编写 , 安装ActivePerl http://downloads.activestate.com/ActivePerl/releases/5.20.1.2000/Act ...
- 【比赛】NOIP2018 保卫王国
DDP模板题 #include<bits/stdc++.h> #define ui unsigned int #define ll long long #define db double ...
- 【arc073f】Many Moves(动态规划,线段树)
[arc073f]Many Moves(动态规划,线段树) 题面 atcoder 洛谷 题解 设\(f[i][j]\)表示第一个棋子在\(i\),第二个棋子在\(j\)的最小移动代价. 发现在一次移动 ...