Problem Description

Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days. 
Now she wonders whether he has a feasible schedule to finish all the tasks in time. She turns to you for help.

Input

On the first line comes an integer T(T<=20), indicating the number of test cases.
You are given two integer N(N<=500) and M(M<=200) on the first line of each test case. Then on each of next N lines are three integers Pi, Si and Ei (1<=Pi, Si, Ei<=500), which have the meaning described in the description. It is guaranteed that in a feasible schedule every task that can be finished will be done before or at its end day.

Output

For each test case, print “Case x: ” first, where x is the case number. If there exists a feasible schedule to finish all the tasks, print “Yes”, otherwise print “No”.
Print a blank line after each test case.

Sample Input

2
4 3
1 3 5
1 1 4
2 3 7
3 5 9
 
2 2
2 1 3
1 2 2

Sample Output

Case 1: Yes
 
Case 2: Yes
解题思路:此题关键在于建图,跑最大流来判断是否已达到满流。题意:有n个任务,m台机器。每个任务有最早才能开始做的时间s_i,截止时间e_i,和完成该任务所需要的时间p_i。每个任务可以分段进行,但在同一天一台机器最多只能执行一个任务,问是否有可行的工作时间来完成所有任务。做法:建立一个超级源点s=0和一个超级汇点t=1001。源点s和每个任务i建边,边权为p_i,表示完成该任务需要的天数。对于每一个任务i,将编号i与编号n+[s_i~e_i]中每个编号建边,边权为1,表示将任务i分在第n+s_i天~第n+e_i天来完成,再将编号n+[s_i~e_i]与汇点t建边,边权为m,表示每一天(编号为n+j)最多同时运行m台机器来完成8天的任务单位量。但由于建的边数太多,用裸的Dinic跑最大流会TLE,怎么优化呢?采用当前弧优化:因为每次dfs找增广路的过程中都是从每个顶点指向的第1(编号为0)条边开始遍历的,而如果第1条边已达到满流,则会继续遍历第2条边....直至找到汇点,事实上这个递归的过程就造成了很多不必要浪费的时间,所以在dfs的过程中应标记一下每个顶点v当前能到达的第curfir[v]条边,说明顶点v的第0条边~第curfir[v]-1条边都已达满流或者流不到汇点,这样时间复杂度就大大降低了。注意:每次bfs给图重新分层次时,需要清空当前弧数组cirfir[],表示初始时从每个顶点v的第1(编号为0)条边开始遍历。
AC代码(124ms):
 #include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
struct edge{ int to,cap;size_t rev;
edge(int _to, int _cap, size_t _rev):to(_to),cap(_cap),rev(_rev){}
};
int T,n,m,p,s,e,tot,level[maxn];queue<int> que;vector<edge> G[maxn];size_t curfir[maxn];//当前弧数组
void add_edge(int from,int to,int cap){
G[from].push_back(edge(to,cap,G[to].size()));
G[to].push_back(edge(from,,G[from].size()-));
}
bool bfs(int s,int t){
memset(level,-,sizeof(level));
while(!que.empty())que.pop();
level[s]=;
que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
for(size_t i=;i<G[v].size();++i){
edge &e=G[v][i];
if(e.cap>&&level[e.to]<){
level[e.to]=level[v]+;
que.push(e.to);
}
}
}
return level[t]<?false:true;
}
int dfs(int v,int t,int f){
if(v==t)return f;
for(size_t &i=curfir[v];i<G[v].size();++i){//从v的第curfir[v]条边开始,采用引用的方法,同时改变本身的值
//因为节点v的第0~curfir[v]-1条边已达到满流了,所以无需重新遍历--->核心优化
edge &e=G[v][i];
if(e.cap>&&(level[v]+==level[e.to])){
int d=dfs(e.to,t,min(f,e.cap));
if(d>){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int max_flow(int s,int t){
int f,flow=;
while(bfs(s,t)){
memset(curfir,,sizeof(curfir));//重新将图分层之后就清空数组,从第0条边开始遍历
while((f=dfs(s,t,INF))>)flow+=f;
}
return flow;
}
int main(){
while(~scanf("%d",&T)){
for(int cas=;cas<=T;++cas){
scanf("%d%d",&n,&m);tot=;
for(int i=;i<maxn;++i)G[i].clear();
for(int i=;i<=;++i)add_edge(+i,,m);
for(int i=;i<=n;++i){
scanf("%d%d%d",&p,&s,&e);
add_edge(,i,p);tot+=p;//tot为总时间
for(int j=s;j<=e;++j)add_edge(i,+j,);
}
printf("Case %d: %s\n\n",cas,max_flow(,)==tot?"Yes":"No");
}
}
return ;
}

解题报告:hdu 3572 Task Schedule(当前弧优化Dinic算法)的更多相关文章

  1. hdu 3572 Task Schedule (dinic算法)

    pid=3572">Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. HDU 3572 Task Schedule(拆点+最大流dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. hdu 3572 Task Schedule 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572 Our geometry princess XMM has stoped her study i ...

  4. HDU 3572 Task Schedule (最大流)

    C - Task Schedule Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. hdu 3572 Task Schedule

    Task Schedule 题意:有N个任务,M台机器.每一个任务给S,P,E分别表示该任务的(最早开始)开始时间,持续时间和(最晚)结束时间:问每一个任务是否能在预定的时间区间内完成: 注:每一个任 ...

  6. hdu 3572 Task Schedule (Dinic模板)

    Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...

  7. hdu 3572 Task Schedule(最大流&amp;&amp;建图经典&amp;&amp;dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. HDU 3572 Task Schedule(ISAP模板&amp;&amp;最大流问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si  , ...

  9. 图论--网络流--最大流 HDU 3572 Task Schedule(限流建图,超级源汇)

    Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...

随机推荐

  1. jquery一个比较好的轮播图jQuery.kinMaxShow介绍

    kinMaxShow API 可选参数以及详解 kinMaxShow 主参数详解 参数名称 默认值 简单释义 height 500 [整型 (单位:像素)]焦点图高度,必须设置 缺省则启用默认高度 5 ...

  2. 修复Xcode升级错误 — PCH File Error

    http://www.rockia.net/2013/03/fix-xcode-update-pch-file-error Error:PCH File Built From A Different ...

  3. HBase协处理器同步二级索引到Solr

    一. 背景二. 什么是HBase的协处理器三. HBase协处理器同步数据到Solr四. 添加协处理器五. 测试六. 协处理器动态加载 一. 背景 在实际生产中,HBase往往不能满足多维度分析,我们 ...

  4. Session Timeout 与 $.ajaxSetup

    对于session过期跳转的问题,很简单,就是一个过滤器,然后判断session为空?跳转:继续.但是对于ajax的请求,需要做特殊处理,见下面代码中的 // 此处考虑ajax操作session过期的 ...

  5. C++使用模板、函数指针、接口和lambda表达式这四种方法做回调函数的区别比较

    在C++中,两个类之间存在一种关系,某个类需要另外一个类去完成某一个功能,完成了之后需要告知该类结果,这种最普通最常见的需求,往往使用回调函数来解决. 如题,我总结下来有这么四种方式可以完成这项功能, ...

  6. HashTable HashMap区分

    主要是安全.速度: 1.HashMap可以接受null的键. 2.HashMap是非synchronized,而Hashtable是synchronized,这意味着Hashtable是线程安全的,多 ...

  7. Mysql语句示例

    Mysql语句示例 最常用 sql 语句总结 前言 Mysql 是数据库开发使用的主要平台之一.sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句 ...

  8. docker hub下载慢解决方法 使用daocloud的mirror

    见:http://blog.csdn.net/dingsai88/article/details/52638758

  9. Android自动化测试环境搭建

    Android自动化环境的搭建主要包括: 1. java jdk和jre的安装和环境的配置 2. appium服务器的安装和配置 3. eclipse开发工具,这里不必要用Android Studio ...

  10. 推箱子 Sokoban(华中农业比赛)

    点这里 打开题目链接   点击打开链接 题目就是我们玩过的推箱子: 一顿暴力广搜:加状态标记.状态压缩需要用到一个类似于康拓的思想来压缩:所以容易TLE,搜索就是用一个int型的数字来表示一个状态, ...