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):
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int INF=0x3f3f3f3f;
  4. const int maxn=;
  5. struct edge{ int to,cap;size_t rev;
  6. edge(int _to, int _cap, size_t _rev):to(_to),cap(_cap),rev(_rev){}
  7. };
  8. int T,n,m,p,s,e,tot,level[maxn];queue<int> que;vector<edge> G[maxn];size_t curfir[maxn];//当前弧数组
  9. void add_edge(int from,int to,int cap){
  10. G[from].push_back(edge(to,cap,G[to].size()));
  11. G[to].push_back(edge(from,,G[from].size()-));
  12. }
  13. bool bfs(int s,int t){
  14. memset(level,-,sizeof(level));
  15. while(!que.empty())que.pop();
  16. level[s]=;
  17. que.push(s);
  18. while(!que.empty()){
  19. int v=que.front();que.pop();
  20. for(size_t i=;i<G[v].size();++i){
  21. edge &e=G[v][i];
  22. if(e.cap>&&level[e.to]<){
  23. level[e.to]=level[v]+;
  24. que.push(e.to);
  25. }
  26. }
  27. }
  28. return level[t]<?false:true;
  29. }
  30. int dfs(int v,int t,int f){
  31. if(v==t)return f;
  32. for(size_t &i=curfir[v];i<G[v].size();++i){//从v的第curfir[v]条边开始,采用引用的方法,同时改变本身的值
  33. //因为节点v的第0~curfir[v]-1条边已达到满流了,所以无需重新遍历--->核心优化
  34. edge &e=G[v][i];
  35. if(e.cap>&&(level[v]+==level[e.to])){
  36. int d=dfs(e.to,t,min(f,e.cap));
  37. if(d>){
  38. e.cap-=d;
  39. G[e.to][e.rev].cap+=d;
  40. return d;
  41. }
  42. }
  43. }
  44. return ;
  45. }
  46. int max_flow(int s,int t){
  47. int f,flow=;
  48. while(bfs(s,t)){
  49. memset(curfir,,sizeof(curfir));//重新将图分层之后就清空数组,从第0条边开始遍历
  50. while((f=dfs(s,t,INF))>)flow+=f;
  51. }
  52. return flow;
  53. }
  54. int main(){
  55. while(~scanf("%d",&T)){
  56. for(int cas=;cas<=T;++cas){
  57. scanf("%d%d",&n,&m);tot=;
  58. for(int i=;i<maxn;++i)G[i].clear();
  59. for(int i=;i<=;++i)add_edge(+i,,m);
  60. for(int i=;i<=n;++i){
  61. scanf("%d%d%d",&p,&s,&e);
  62. add_edge(,i,p);tot+=p;//tot为总时间
  63. for(int j=s;j<=e;++j)add_edge(i,+j,);
  64. }
  65. printf("Case %d: %s\n\n",cas,max_flow(,)==tot?"Yes":"No");
  66. }
  67. }
  68. return ;
  69. }

解题报告: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. javascript常用事件及方法

    1.获取鼠标坐标,考虑滚动条拖动 var e = event || window.event; var scrollX = document.documentElement.scrollLeft || ...

  2. javascript if(xx)

    js判断某个值知否存在或者为空,可以直接用if(xx)过滤. <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  3. ReactMotion Demo8 分析

    链接 首先通过spring函数Motion的style参数, 传入Motion Component, 计算style的过程: const style = lastPressed === i & ...

  4. java24点算法

    输入任意的四个数,求出所有能得到二十四点的算式,不过我是菜鸟,可能性能方面不好,希望各位多多指教​1. [代码][Java]代码     import java.util.ArrayList;impo ...

  5. 一步一步学Silverlight 2系列(26):基本图形

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. Ural 1109 Conference(最小路径覆盖数)

    题意:A国家有M个代表,B国有N个代表,其中有K对代表可以进行谈判(一个是A国的,一个是B国的),并且每一个代表至少被包含在其中一对中(也就是说,每个人可以至少找到另外一个人谈判),每一对谈判需要一对 ...

  7. select下拉带图片-模拟下拉

    <style> /*下拉列表*/ ul,dl,ol,li {list-style: none;} .dropdown { float: right; position: relative; ...

  8. Flask app.config 的配置

    原理如下:   image.png 1.通过调用自定义config.py文件中config字典,可以得到一个类, 这个类里面定义的都是类变量,这些变量就是自定义的一些配置项. 如下config.py ...

  9. c/c++内存机制(一)(原)

    一:C语言中的内存机制 在C语言中,内存主要分为如下5个存储区: (1)栈(Stack):位于函数内的局部变量(包括函数实参),由编译器负责分配释放,函数结束,栈变量失效. (2)堆(Heap):由程 ...

  10. 【转】Intellij idea 的maven项目如何通过maven自动下载jar包

    原文地址: https://blog.csdn.net/machao0903/article/details/73368909 maven项目自动加载jar包 所需工具如下: Intellij IDE ...