不要过于沉溺过去,也不要过于畅想未来,把握现在!

HDU 3572 Dinic

Task Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4323    Accepted Submission(s): 1447


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
 

 

Author
allenlowesy
 

 

Source

思路:

1.主要是建图,0作为源点,1n设置为任务,0到任务的边权为任务所需的天数,nn+最大截止日期(top)设置为每一天,任务到相关每一天的边权设置为1,n+top+1设置为汇点,每一天到汇点的边权设置为机器数m;

2.直接把0作为源点,1-500作为任务,501-1000作为天数,1001作为汇点,建图会稍微简单一点;

3.判满流;

 
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstring>
  5 #include<vector>
  6 #include<queue>
  7 #include<algorithm>
  8 using namespace std;
  9 const int inf=0x3fffffff;
 10 const int maxn=1007;
 11 struct Edge
 12 {
 13     int from,to,cap,flow;
 14     Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){};
 15 };
 16 
 17 struct Dinic{
 18   int n,m,s,t;
 19   vector<Edge> edges;
 20   vector<int> G[maxn];
 21   bool vis[maxn];
 22   int d[maxn];
 23   int cur[maxn];
 24   void init()
 25   {
 26       for(int i=0;i<maxn;i++)
 27       {
 28           G[i].clear();
 29       }
 30       edges.clear();
 31   }
 32   void AddEdge(int from,int to,int cap)
 33 {
 34     edges.push_back(Edge(from,to,cap,0));
 35     edges.push_back(Edge(to,from,0,0));
 36     int mm=edges.size();
 37     G[from].push_back(mm-2);
 38     G[to].push_back(mm-1);
 39 }
 40   bool BFS(){
 41   memset(vis,0,sizeof(vis));
 42   queue<int>Q;
 43   Q.push(s);
 44   d[s]=0;
 45   vis[s]=1;
 46   while(!Q.empty()){
 47     int x=Q.front();Q.pop();
 48     for(int i=0;i<G[x].size();i++){
 49         Edge &e=edges[G[x][i]];
 50         if(!vis[e.to]&&e.cap>e.flow){
 51             vis[e.to]=1;
 52             d[e.to]=d[x]+1;
 53             Q.push(e.to);
 54         }
 55     }
 56   }
 57   return vis[t];
 58   }
 59   int DFS(int x,int a){
 60   if(x==t||a==0) return a;
 61   int flow=0,f;
 62   for(int &i=cur[x];i<G[x].size();i++){
 63     Edge &e=edges[G[x][i]];
 64     if(d[e.to]==d[x]+1&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
 65         e.flow+=f;
 66         edges[G[x][i]^1].flow-=f;
 67         flow+=f;
 68         a-=f;
 69         if(a==0) break;
 70     }
 71   }
 72   return flow;
 73   }
 74   int Maxflow(int s,int t){
 75   this->s=s;this->t=t;
 76   int flow=0;
 77   while(BFS()){
 78     memset(cur,0,sizeof(cur));
 79     flow+=DFS(s,inf);
 80   }
 81   return flow;
 82   }
 83 };
 84 Dinic dic;
 85 int main()
 86 {
 87    // freopen("in.txt","r",stdin);
 88     int tt,cnt=1;int n,s,a,b,c,t,m;
 89     scanf("%d",&tt);
 90     int top=-1;
 91     int sum;
 92     while(tt--){
 93         dic.init();
 94         sum=0;
 95         scanf("%d%d",&n,&m);
 96         for(int i=1;i<=n;i++)
 97         {
 98             scanf("%d%d%d",&a,&b,&c);
 99             sum+=a;
100             if(c>top) top=c;
101             dic.AddEdge(0,i,a);
102             for(int j=b;j<=c;j++)
103             {
104                 dic.AddEdge(i,n+j,1);
105             }
106         }
107         for(int i=n+1;i<=n+top;i++)
108         {
109             dic.AddEdge(i,n+top+1,m);
110         }
111         s=0,t=n+top+1;
112         int ans=dic.Maxflow(s,t);
113         if(ans==sum) printf("Case %d: Yes\n\n",cnt++);
114         else printf("Case %d: No\n\n",cnt++);
115     }
116 }

 

posted @ 2015-01-30 22:07  coding_yuan  阅读(250)  评论(0编辑  收藏  举报

不要过于沉溺过去,也不要过于畅想未来,把握现在!