博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3572 Task Schedule(拆点+最大流dinic)
阅读量:4611 次
发布时间:2019-06-09

本文共 4276 字,大约阅读时间需要 14 分钟。

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 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
 

 

题目链接:

拆点的最大流判断是否满流的题目,点怎么拆呢?从源点S连向每一个任务i一条容量为p的边,说明每一个任务一开始要p个流量流入,然后每一个任务i向时间点[s,e]连一条容量为1的边,说明一个任务只能同时在一个时间点被工作,即不能同时既在时间点A上加工又在时间点B上加工,然后每一个时间点向T连一条容量为m个边,说明一个时间点只能最多同时有m个机器在工作。最后你就是要判断从S流出的$n*p$个流量能否全部流入T中就好了

空间复杂度大概是$(500+500^2+500)*2$条边,$500+500$个点,原本只会最辣鸡的FF想低空卡过这题,然而被无限TLE教做人,查查题解又膜膜dinic,发现dinic也容易理解,分层的意义就是减少没有用的搜索,因为增广一定是从最小距离距离近的到最小距离远的,那么那些d[v]!=d[u]+1的点就可以被忽略掉了

代码:

#include 
#include
using namespace std;#define INF 0x3f3f3f3f#define CLR(arr,val) memset(arr,val,sizeof(arr))#define LC(x) (x<<1)#define RC(x) ((x<<1)+1)#define MID(x,y) ((x+y)>>1)typedef pair
pii;typedef long long LL;const double PI=acos(-1.0);const int N=1010;const int M=251000+7;struct edge{ int to,nxt; int cap;};edge E[M<<1];int head[N],tot,d[N];void add(int s,int t,int cap){ E[tot].to=t; E[tot].cap=cap; E[tot].nxt=head[s]; head[s]=tot++; E[tot].to=s; E[tot].cap=0; E[tot].nxt=head[t]; head[t]=tot++;}void init(){ CLR(head,-1); tot=0;}int bfs(int s,int t){ CLR(d,-1); d[s]=0; queue
Q; Q.push(s); while (!Q.empty()) { int now=Q.front(); Q.pop(); for (int i=head[now]; ~i; i=E[i].nxt) { int v=E[i].to; if(d[v]==-1&&E[i].cap>0) { d[v]=d[now]+1; if(v==t) return 1; Q.push(v); } } } return d[t]!=-1;}int dfs(int s,int t,int f){ if(s==t||!f) return f; int r=0; for (int i=head[s]; ~i; i=E[i].nxt) { int v=E[i].to; if(d[v]==d[s]+1&&E[i].cap) { int d=dfs(v,t,min(f,E[i].cap)); if(d>0) { E[i].cap-=d; E[i^1].cap+=d; r+=d; f-=d; if(!f) break; } } } if(!r) d[s]=INF; return r;}int dinic(int s,int t){ int r=0; while (bfs(s,t)) r+=dfs(s,t,INF); return r;}int main(void){ int tcase,p,s,e,i,j,n,m; scanf("%d",&tcase); for (int q=1; q<=tcase; ++q) { init(); scanf("%d%d",&n,&m); int S=0; int tl=INF,tr=-INF; int sump=0; for (i=1; i<=n; ++i) { scanf("%d%d%d",&p,&s,&e); add(S,i,p); sump+=p; if(s
tr) tr=e; for (j=s; j<=e; ++j) add(i,n+j,1); } int T=n+tr+1; for (i=tl; i<=tr; ++i) add(n+i,T,m); printf("Case %d: %s\n\n",q,dinic(S,T)==sump?"Yes":"No"); } return 0;}

转载于:https://www.cnblogs.com/Blackops/p/6155319.html

你可能感兴趣的文章
3、流程语句相关练习
查看>>
30、git 使用
查看>>
iOS网络-02-数据解析(JSON与XML)
查看>>
python列表求和的几种等效电路
查看>>
Luogu P3393 逃离僵尸岛
查看>>
Flatten Binary Tree to Linked List
查看>>
Edit Distance
查看>>
软件工程第一次作业补充
查看>>
N76E003---输入捕获
查看>>
poj 1094 Sorting It All Out(拓扑排序)
查看>>
acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
查看>>
BMP图像格式
查看>>
python的匿名函数lambda解释及用法
查看>>
c#遍历Dictionary使用KeyValuePair
查看>>
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>