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.
InputOn 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.
OutputFor 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

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include<queue>
  5 
  6 using namespace std;
  7 
  8 #define M 1100
  9 #define inf 0x3f3f3f3f
 10 
 11 int head[M],dis[M];
 12 int n,m,t,cnt;
 13 
 14 struct node{
 15     int u,v,next,w;
 16 }mp[M*M];
 17 
 18 void add(int u,int v,int w){
 19     mp[cnt].u=u;
 20     mp[cnt].v=v;
 21     mp[cnt].w=w;
 22     mp[cnt].next=head[u];
 23     head[u]=cnt++;
 24     mp[cnt].u=v;
 25     mp[cnt].v=u;
 26     mp[cnt].w=0;
 27     mp[cnt].next=head[v];
 28     head[v]=cnt++;
 29 }
 30 int bfs(){
 31     int s=0;
 32     memset(dis,-1,sizeof(dis));
 33     queue <int> q;
 34     while(!q.empty()) q.pop();
 35     dis[s]=0;
 36     q.push(s);
 37     while(!q.empty()){
 38         s=q.front();
 39         q.pop();
 40         for(int i=head[s];i!=-1;i=mp[i].next){
 41             int v=mp[i].v;
 42             if(dis[v]==-1&&mp[i].w){
 43                 dis[v]=dis[s]+1;
 44                 if(v==t) return 1;
 45                 q.push(v);
 46             }
 47         }
 48     }
 49     return 0;
 50 }
 51 int dfs(int s,int low){
 52     if(s==t) return low;
 53     int a,i,ans=0;
 54     for(i=head[s];i!=-1;i=mp[i].next){
 55         int v=mp[i].v;
 56         if(mp[i].w && dis[v]==dis[s]+1 && (a=dfs(v,min(low,mp[i].w))) ){
 57             mp[i].w-=a;
 58             mp[i^1].w+=a;
 59             ans+=a; 
 60             if(ans==low) break;
 61         }
 62     }
 63     return ans;
 64 }
 65 
 66 int dinic()
 67 {
 68     int ans=0,k;
 69     while(bfs())
 70     {
 71         while(k=dfs(0,inf))
 72             ans+=k;
 73     }    
 74     return ans;
 75 }
 76 
 77 int main()
 78 {
 79     int T,t1,t2,sum,cas=1;
 80     scanf("%d",&T);
 81     while(T--)
 82     {
 83         int s[505],e[505],q[505];
 84         scanf("%d%d",&n,&m);
 85         t1=inf;
 86         t2=-1;
 87         sum=0;
 88         for(int i=1;i<=n;i++)
 89         {
 90             scanf("%d%d%d",&q[i],&s[i],&e[i]);
 91             t1=min(t1,s[i]);
 92             t2=max(t2,e[i]);
 93             sum+=q[i];
 94         }
 95         memset(head,-1,sizeof(head));
 96         cnt=0; t=t2*2+1;
 97         for(int i=t1;i<=t2;i++)
 98             add(0,i,m);
 99         for(int i=1;i<=n;i++){
100             for(int j=s[i];j<=e[i];j++){
101                 add(j,j+t2,1);
102                 add(j+t2,t2*2+1,m);
103             }
104         } 
105         int ans=0,k;
106         ans=dinic();
107         if(sum<=ans)
108             printf("Case %d: Yes\n\n",cas++);
109         else printf("Case %d: No\n\n",cas++);        
110         
111     }    
112     
113     return 0;
114 }

 

posted @ 2017-08-25 08:20  西北会法语  阅读(105)  评论(0编辑  收藏  举报