题目链接

给你n种菜, 每一种可以开始吃的时间不一样, 结束的时间也不一样。 求每种菜吃的时间都相同的最大的时间。时间的范围是0-10000。

看到这个题明显可以想到网络流, 但是时间的范围明显不允许我们把对每一个时间都连边。

那么我们可以对时间的区间连边, 对于一个区间, 如果一盘菜的开始时间>=这个区间的左端点, 结束时间小于右端点, 那么这盘菜就对这个时间段连边, 权值inf。然后每个时间段对汇点t连边, 权值为这段时间的长度。 源点s向每个菜连边, 权值为二分枚举的时间长度, 然后跑最大流, 看结果是否等于n*x, x是枚举的时间。

具体对时间区间连边的方法可以看代码。

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 4e5+;
int q[maxn*], head[maxn*], dis[maxn/], s, t, used[], a[], b[], n, cnt, num, from;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
void init() {
num = from = cnt = ;
mem1(head);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = ;
int st = , ed = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
int check(int x) {
init();
for(int i = ; i<; i++) {
if(used[i]) {
int flag = ;
for(int j = ; j<n; j++) {
if(a[j]<=from&&i<=b[j]) {
if(flag) {
add(s, ++cnt, i-from); //起点对区间连边。
flag = ;
}
add(cnt, j+, inf); //区间对每一个在它范围内的菜连边
}
}
from = i;
}
}
for(int i = ; i<n; i++) {
add(i+, t, x);
}
int ans = dinic();
if(ans == x*n)
return ;
return ;
}
int main()
{
cin>>n;
for(int i = ; i<n; i++) {
scanf("%d%d", &a[i], &b[i]);
used[a[i]] = ;
used[b[i]] = ;
}
s = , t = ;
int l = , r = ;
while(r>l+) {
int mid = l+r>>;
if(check(mid)) {
l = mid;
} else {
r = mid;
}
}
cout<<l*n<<endl;
}

codeforces 589F. Gourmet and Banquet 二分+网络流的更多相关文章

  1. Codeforces 589F Gourmet and Banquet

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...

  2. Codeforces 653D Delivery Bears【二分+网络流】

    题目链接: http://codeforces.com/problemset/problem/653/D 题意: x个熊拿着相同重量的物品,从1号结点沿着路走到N号结点,结点之间有边相连,保证可以从1 ...

  3. codeforces 653D D. Delivery Bears(二分+网络流)

    题目链接: D. Delivery Bears time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. 【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)

    F. Gourmet and Banquet time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  5. hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

    #1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...

  6. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: ...

  7. BZOJ_3993_[SDOI2015]星际战争_二分+网络流

    BZOJ_3993_[SDOI2015]星际战争_二分+网络流 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进 ...

  8. 【bzoj3130】[Sdoi2013]费用流 二分+网络流最大流

    题目描述 Alice和Bob做游戏,给出一张有向图表示运输网络,Alice先给Bob一种最大流方案,然后Bob在所有边上分配总和等于P的非负费用.Alice希望总费用尽量小,而Bob希望总费用尽量大. ...

  9. 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

    题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...

随机推荐

  1. 获取UIButton的一些属性

    获取文字  button.currentTitle 更多如下: @property(nullable, nonatomic,readonly,strong) NSString *currentTitl ...

  2. iOS多线程的基本使用

    一.NSThread: 程序就是一段代码,是静态的概念 进程是运行起来的程序,是动态的概念,进程需要占内存空间 线程是进程的基本单位,一个进程至少有一个线程,iOS程序默认有一个主线程,用来显示和操作 ...

  3. JavaScript的一点简介(注:本文诸多观点源于JavaScript高级程序设计,如有侵权,立即删除)

    JavaScript是一门最易让人误解的语言,该语言中精华与糟粕并存(可能比一般语言的这个比例要大一些):但“千淘万漉虽辛苦,吹尽黄沙始到金”,层层面纱下是易用灵活.优雅轻灵的内在.很久以前,Java ...

  4. OSC本地库推送到远程库

    1.新建远程库: 例如:http://git.oschina.net/intval/learngit 2.本地生成ssh密钥 ssh-keygen -t rsa -C "intval@163 ...

  5. PHP基于变量的引用实现的树状结构

    直接上代码: function aryTree($ary, $tagId = 'id', $tagPid = 'pid', $tagSub = '_sub') { if(is_array($ary)) ...

  6. JPEG图像密写研究(一) JPEG图像文件结构

    [转载]转载自http://www.cnblogs.com/leaven/archive/2010/04/06/1705846.html JPEG压缩编码算法的主要计算步骤如下: (0) 8*8分块. ...

  7. onActivityResult不起作用?可能是和你的launchMode有关!

    昨天在自己的项目中用到了onActivityResult()方法获得activity的返回值.我从Activity01通过 startActivityForResult启动了Activity02和Ac ...

  8. InputStream、OutputStream、String的相互转换(转)

    //1.字符串转inputStream String string; //...... InputStream is = new ByteArrayInputStream(string.getByte ...

  9. WEKA,一个开源java的数据挖掘工具

    开始研究WEKA,一个开源java的数据挖掘工具. HS沉寂这么多天,谁知道偏偏在我申请离职的时候给我安排了个任务,哎,无语. 于是,今天看了一天的Weka. 主要是看了HS提供的三个文章(E文,在g ...

  10. C语言入门(6)——C语言常用数学函数

    在编码过程中会经遇到数学运算,幸运的是C语言提供了非常丰富的数学函数库. 在数学中使用函数有时候书写可以省略括号,而C语言要求一定要加上括号,例如sin(pi/2)这种形式.在C语言的术语中,pi/2 ...