Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4624 Accepted Submission(s): 1516
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
#include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<climits> #define Max(a,b) a>b?a:b #define Min(a,b) a<b?a:b using namespace std; struct Edge { int s,t,f,next; }edge[1100000]; int head[1010]; int cur[1010]; int pre[1010]; int stack[1100000]; int ent; int n,m,times,s,t; void add(int start,int last,int f) { edge[ent].s=start;edge[ent].t=last;edge[ent].f=f;edge[ent].next=head[start];head[start]=ent++; edge[ent].s=last;edge[ent].t=start;edge[ent].f=0;edge[ent].next=head[last];head[last]=ent++; } bool bfs(int S,int T) { memset(pre,-1,sizeof(pre)); pre[S]=0; queue<int>q; q.push(S); while(!q.empty()) { int temp=q.front(); q.pop(); for(int i=head[temp];i!=-1;i=edge[i].next) { int temp2=edge[i].t; if(pre[temp2]==-1&&edge[i].f) { pre[temp2]=pre[temp]+1; q.push(temp2); } } } return pre[T]!=-1; } int dinic(int start,int last) { int flow=0,now; while(bfs(start,last)) { int top=0; memcpy(cur,head,sizeof(head)); int u=start; while(1) { if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流 { int minn=INT_MAX; for(int i=0;i<top;i++) { if(minn>edge[stack[i]].f) { minn=edge[stack[i]].f; now=i; } } flow+=minn; for(int i=0;i<top;i++) { edge[stack[i]].f-=minn; edge[stack[i]^1].f+=minn; } top=now; u=edge[stack[top]].s; } for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)//找出从u点出发能到的边 if(edge[i].f&&pre[edge[i].t]==pre[u]+1) break; if(cur[u]==-1)//如果从该点未找到可行边,将该点标记并回溯 { if(top==0)break; pre[u]=-1; u=edge[stack[--top]].s; } else//如果找到了继续运行 { stack[top++]=cur[u]; u=edge[cur[u]].t; } } } return flow; } int main() { scanf("%d",×); for(int cas=1;cas<=times;cas++) { ent=0; memset(head,-1,sizeof(head)); int st,ed,lt; int maxn=0; int sum=0; s=0;t=1001; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d%d%d",<,&st,&ed); maxn=Max(maxn,ed); for(int j=st;j<=ed;j++) add(j,i+500,1); add(i+500,t,lt); sum+=lt; } for(int i=1;i<=maxn;i++) add(s,i,m); if(dinic(s,t)==sum)printf("Case %d: Yes\n",cas); else printf("Case %d: No\n",cas); printf("\n"); } return 0; }