HDU 3572 Dinic

Task Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4323    Accepted Submission(s): 1447

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

Author

allenlowesy

Source

2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC

思路:

1.主要是建图,0作为源点,1到n设置为任务,0到任务的边权为任务所需的天数,n到n+最大截止日期(top)设置为每一天,任务到相关每一天的边权设置为1,n+top+1设置为汇点,每一天到汇点的边权设置为机器数m;

2.直接把0作为源点,1-500作为任务,501-1000作为天数,1001作为汇点,建图会稍微简单一点;

3.判满流;

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstring>
  5 #include<vector>
  6 #include<queue>
  7 #include<algorithm>
  8 using namespace std;
  9 const int inf=0x3fffffff;
 10 const int maxn=1007;
 11 struct Edge
 12 {
 13     int from,to,cap,flow;
 14     Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){};
 15 };
 16
 17 struct Dinic{
 18   int n,m,s,t;
 19   vector<Edge> edges;
 20   vector<int> G[maxn];
 21   bool vis[maxn];
 22   int d[maxn];
 23   int cur[maxn];
 24   void init()
 25   {
 26       for(int i=0;i<maxn;i++)
 27       {
 28           G[i].clear();
 29       }
 30       edges.clear();
 31   }
 32   void AddEdge(int from,int to,int cap)
 33 {
 34     edges.push_back(Edge(from,to,cap,0));
 35     edges.push_back(Edge(to,from,0,0));
 36     int mm=edges.size();
 37     G[from].push_back(mm-2);
 38     G[to].push_back(mm-1);
 39 }
 40   bool BFS(){
 41   memset(vis,0,sizeof(vis));
 42   queue<int>Q;
 43   Q.push(s);
 44   d[s]=0;
 45   vis[s]=1;
 46   while(!Q.empty()){
 47     int x=Q.front();Q.pop();
 48     for(int i=0;i<G[x].size();i++){
 49         Edge &e=edges[G[x][i]];
 50         if(!vis[e.to]&&e.cap>e.flow){
 51             vis[e.to]=1;
 52             d[e.to]=d[x]+1;
 53             Q.push(e.to);
 54         }
 55     }
 56   }
 57   return vis[t];
 58   }
 59   int DFS(int x,int a){
 60   if(x==t||a==0) return a;
 61   int flow=0,f;
 62   for(int &i=cur[x];i<G[x].size();i++){
 63     Edge &e=edges[G[x][i]];
 64     if(d[e.to]==d[x]+1&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
 65         e.flow+=f;
 66         edges[G[x][i]^1].flow-=f;
 67         flow+=f;
 68         a-=f;
 69         if(a==0) break;
 70     }
 71   }
 72   return flow;
 73   }
 74   int Maxflow(int s,int t){
 75   this->s=s;this->t=t;
 76   int flow=0;
 77   while(BFS()){
 78     memset(cur,0,sizeof(cur));
 79     flow+=DFS(s,inf);
 80   }
 81   return flow;
 82   }
 83 };
 84 Dinic dic;
 85 int main()
 86 {
 87    // freopen("in.txt","r",stdin);
 88     int tt,cnt=1;int n,s,a,b,c,t,m;
 89     scanf("%d",&tt);
 90     int top=-1;
 91     int sum;
 92     while(tt--){
 93         dic.init();
 94         sum=0;
 95         scanf("%d%d",&n,&m);
 96         for(int i=1;i<=n;i++)
 97         {
 98             scanf("%d%d%d",&a,&b,&c);
 99             sum+=a;
100             if(c>top) top=c;
101             dic.AddEdge(0,i,a);
102             for(int j=b;j<=c;j++)
103             {
104                 dic.AddEdge(i,n+j,1);
105             }
106         }
107         for(int i=n+1;i<=n+top;i++)
108         {
109             dic.AddEdge(i,n+top+1,m);
110         }
111         s=0,t=n+top+1;
112         int ans=dic.Maxflow(s,t);
113         if(ans==sum) printf("Case %d: Yes\n\n",cnt++);
114         else printf("Case %d: No\n\n",cnt++);
115     }
116 }
时间: 2024-10-26 14:28:43

HDU 3572 Dinic的相关文章

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

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7753    Accepted Submission(s): 2381 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

hdu 3572 Task Schedule(最大流)

hdu 3572 Task Schedule 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 tas

HDU 3572 Task Schedule (最大流)

C - Task Schedule Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3572 Description Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened fac

HDU 3572 Task Schedule(ISAP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572 题意:m台机器,需要做n个任务.第i个任务,你需要使用机器Pi天,且这个任务要在[Si  ,  Ei]区间内完成才有效.对于一个任务,只能由一个机器来完成,一个机器同一时间只能做一个任务.当然,一个任务可以分成几段不连续的时间来完成.问,能否做完全部任务. 题意很清晰,也就是判断是否是满流. 对于网络流问题,模板大家都有,关键在于如何建图(详见资料) 思路:今天问了龙哥,对建图有了一定的了解,

HDU 3987 &amp;&amp; DINIC

很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流,但有可能是最少边数最少割的边呢?否.因为按照DINIC的求法,每次都是增广容量最少的路,若当前不满流,则必定不是最小割的边,所以只需在满流的边,即可组成最小割的边寻找最少边就可以了. 1 #include <iostream> 2 #include <cstring> 3 #incl

HDU 3572 Task Schedule(最大流Dinic算法)

该题非常经典,建图的方法非常巧妙,因为每个任务的完成不一定要连续,而且可以换机器完成,而且我们注意到时间点最多500,很小,所以我们将时间点抽出来建图. 对于每个任务,将其时间范围内的点与之连起来,容量显然为1 ,并与汇点相连,容量为p[i] .  对于每个时间点,因为最多可以有m台机器同时工作,所以容量为m . 一开始老想着任务和机器之间怎么建立联系了. 推荐题目: 点击打开链接 细节参见代码: #include<bits/stdc++.h> using namespace std; typ

hdu 3572 Task Schedule (Dinic模板)

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

hdu 3572 Task Schedule 最大流 Dinic算法,,卡时间。。建图非常有讲究

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4617    Accepted Submission(s): 1513 Problem Description Our geometry princess XMM has stoped her study in computational geometry t