HDU 3572 Task Schedule
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
纯最大流,建图后求一下最大流,与ΣPi比较一下就可以。
难点在建图。
借用http://blog.csdn.net/luyuncheng/article/details/7944417的图片:
第一层的边权是Pi,第二层是1,第三层是M。
用FF会超时,SAP过的,所以也留一份SAP的模板。
#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>
#define N 1010
int head[N],cnt,ans;
int gap[N],curedge[N],d[N],pre[N];
// gap:统计高度数量数组,d:距离标号数组;
// curedges:当前弧数组;pre:前驱数组
struct node{
int cap,to;
int next;
}edge[502000];
void initi(){
memset(d,0,sizeof(d));
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
memset(head,-1,sizeof(head));
ans=0;//初始化最大流为0
cnt=0;
}
void addedge(int a,int b,int c){//有向图加边
edge[cnt].to=b,edge[cnt].cap=c;
edge[cnt].next=head[a],head[a]=cnt++;
edge[cnt].to=a,edge[cnt].cap=0;
edge[cnt].next=head[b],head[b]=cnt++;
}
int max_flow(int start,int end,int n){
int i,u,tmp,neck;
for(i=1;i<=n;i++)
curedge[i]=head[i];//初始化当前弧为第一条邻接边
gap[0]=n;
u=start;
while(d[start]<n){//当d[start]>=n,网络中肯定出现了gap
if(u==end){//增广成功,寻找瓶颈边
int min_flow=INT_MAX;
for(i=start;i!=end;i=edge[curedge[i]].to){
if(min_flow>edge[curedge[i]].cap){
neck=i;
min_flow=edge[curedge[i]].cap;
}
}
for(i=start;i!=end;i=edge[curedge[i]].to){//更新边与回退边的流量
tmp=curedge[i];
edge[tmp].cap-=min_flow;
edge[tmp^1].cap+=min_flow;//^1:偶数+1,奇数-1
}
ans+=min_flow;
u=neck;//下次增广从瓶颈边开始
}
for(i=curedge[u];i!=-1;i=edge[i].next)
if(edge[i].cap&&d[u]==d[edge[i].to]+1)//寻找可行弧
break;
if(i!=-1){
curedge[u]=i;
pre[edge[i].to]=u;
u=edge[i].to;
}
else{
if(--gap[d[u]]==0)
break;
curedge[u]=head[u];
for(tmp=n,i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cap)
tmp=std::min(tmp,d[edge[i].to]);
d[u]=tmp+1;
++gap[d[u]];
if(u!=start)
u=pre[u];//重标号并且从当前点前驱重新增广
}
}
return ans;
}
int main(){
int n,m;
int T,k;
scanf("%d",&T);
for(k=1;k<=T;k++){
scanf("%d %d",&n,&m);
int i,j,p,s,e,sum=0,maxs=0,flow;
initi();//算法开始前,一定要初始化
for(i=1;i<=n;i++){
scanf("%d %d %d",&p,&s,&e);
sum+=p;
if(maxs<e)
maxs=e;
addedge(1,i+1,p);
for(j=s;j<=e;j++){
addedge(i+1,n+j+1,1);
}
}
for(i=1;i<=maxs;i++){
addedge(n+i+1,n+maxs+2,m);
}
flow=max_flow(1,n+maxs+2,n+maxs+2);
if(flow==sum)
printf("Case %d: Yes\n\n",k);
else
printf("Case %d: No\n\n",k);
}
return 0;
}