Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13372 Accepted Submission(s): 4030
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
一道比较裸的最大流。
看着挺像贪心的,如果不是网络流专题看到,应该会想贪心的做法。
考虑建图:因为任务比较少,我们把500天全部拿出来,让S连向每一天,流量为m,因为一共有m台机器,一天最多有m台机器同时进行。然后对于每个任务的st和ed,对于任务,让每一天连向这个任务,然后任务连向T,流量为p。
AC代码:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=10010,M=1000010;
int T,n,m,h[N],ts,s,t,res;
int head[N],nex[M],to[M],w[M],tot=1;
inline void ade(int a,int b,int c){
to[++tot]=b; nex[tot]=head[a]; w[tot]=c; head[a]=tot;
}
inline void add(int a,int b,int c){
ade(a,b,c); ade(b,a,0);
}
inline int bfs(){
memset(h,0,sizeof head); queue<int> q; q.push(s); h[s]=1;
while(q.size()){
int u=q.front(); q.pop();
for(int i=head[u];i;i=nex[i]){
if(w[i]&&!h[to[i]]){
h[to[i]]=h[u]+1; q.push(to[i]);
}
}
}
return h[t];
}
int dfs(int x,int f){
if(x==t) return f; int fl=0;
for(int i=head[x];i&&f;i=nex[i]){
if(w[i]&&h[to[i]]==h[x]+1){
int mi=dfs(to[i],min(w[i],f));
w[i]-=mi; w[i^1]+=mi; f-=mi; fl+=mi;
}
}
if(!fl) h[x]=-1;
return fl;
}
inline int dinic(){
int ret=0;
while(bfs()) ret+=dfs(s,inf);
return ret;
}
signed main(){
cin>>T;
while(T--){
cin>>n>>m; s=res=0; t=n+501; tot=1; memset(head,0,sizeof head);
for(int i=1;i<=500;i++) add(s,i,m);
for(int i=1;i<=n;i++){
int s,p,e; cin>>p>>s>>e; res+=p;
for(int j=s;j<=e;j++) add(j,500+i,1);
add(500+i,t,p);
}
printf("Case %d: ",++ts);
puts(dinic()==res?"Yes":"No");puts("");
}
return 0;
}