码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3572 Task Schedule

时间:2014-11-24 22:13:59      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

Task Schedule

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3572
64-bit integer IO format: %I64d      Java class name: Main
 
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

Source

 
解题:最大流。。建图确实有点。。。一下子想不到。。。还以为要拆边。。。
 
好吧 源点到每个任务建边容量为pi,每个任务到期限内的各天连容量为1的边,每天到汇点连容量为m的边。。。
 
现在很好理解了,如果满流即存在可行方案。。
 
每天最多m台机器同时工作。。。每个任务需要工作pi天。。。
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 20000;
18 struct arc{
19     int to,flow,next;
20     arc(int x = 0,int y = 0,int z = -1){
21         to = x;
22         flow = y;
23         next = z;
24     }
25 };
26 arc e[maxn*10];
27 int head[maxn],d[maxn],cur[maxn];
28 int tot,S,T,n,m;
29 void add(int u,int v,int flow){
30     e[tot] = arc(v,flow,head[u]);
31     head[u] = tot++;
32     e[tot] = arc(u,0,head[v]);
33     head[v] = tot++;
34 }
35 bool bfs(){
36     memset(d,-1,sizeof(d));
37     queue<int>q;
38     d[T] = 1;
39     q.push(T);
40     while(!q.empty()){
41         int u = q.front();
42         q.pop();
43         for(int i = head[u]; ~i; i = e[i].next){
44             if(e[i^1].flow && d[e[i].to] == -1){
45                 d[e[i].to] = d[u] + 1;
46                 q.push(e[i].to);
47             }
48         }
49     }
50 
51     return d[S] > -1;
52 }
53 int dfs(int u,int low){
54     if(u == T) return low;
55     int tmp = 0,a;
56     for(int &i = cur[u]; ~i; i = e[i].next){
57         if(e[i].flow && d[e[i].to] + 1 == d[u]&&(a=dfs(e[i].to,min(e[i].flow,low)))){
58             e[i].flow -= a;
59             e[i^1].flow += a;
60             tmp += a;
61             low -= a;
62             if(!low) break;
63         }
64     }
65     if(!tmp) d[u] = -1;
66     return tmp;
67 }
68 int dinic(){
69     int ans  = 0;
70     while(bfs()){
71         memcpy(cur,head,sizeof(head));
72         ans += dfs(S,INF);
73     }
74     return ans;
75 }
76 int main() {
77     int cs,pi,si,ei,cao = 1;
78     scanf("%d",&cs);
79     while(cs--){
80         scanf("%d %d",&n,&m);
81         int mx = 0,sum = 0;
82         memset(head,-1,sizeof(head));
83         S = tot = 0;
84         for(int i = 1; i <= n; ++i){
85             scanf("%d %d %d",&pi,&si,&ei);
86             sum += pi;
87             for(int k = si; k <= ei; ++k) add(i,n+k,1);
88             add(S,i,pi);
89             mx = max(mx,ei);
90         }
91         T = n + mx + 1;
92         for(int i = n+1; i < T; ++i) add(i,T,m);
93         printf("Case %d: %s\n\n",cao++,dinic() == sum?"Yes":"No");
94     }
95     return 0;
96 }
View Code

 

HDU 3572 Task Schedule

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/crackpotisback/p/4119649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!