Boss Xnby’s Scheduling Problem | ||||||
|
||||||
Description | ||||||
Boss xnby owns a company, as you know, in the company there are lots of jobs to do.Each job J has a processing requirement pj (denoting the number of machine days required to complete the job), a release date rj (representing the beginning of the day when job j becomes avalible for processing), and a due date dj ≥ rj + pj (representing the beginning of the day by which the job must be completed). What could we do when facing so many jobs? Thank goodness, in xnby’s company, there are some parallel machines can do these jobs.Since a machine can work on only one job at a time and each job can be processed by at most one machine at a time.And preemptions(i.e., we can interrupt a job and process it on different machines on different days) is allowed.Now xnby can use these parallel machines to process these boring jobs,but he also need to determine a feasible schedule that completes all jobs before their due dates or show no such schedule existd.Xnby is a boss, he is a big shot, he had no time to do with these trivial things, so he arranged for you to do this task. |
||||||
Input | ||||||
An integer T (T ≤ 100) indicated the number of test cases. For each test cases: Two integers J and M (J ≤ 100, M ≤ 100) denote jobs and machines respectively. In the following J lines(each job one line, in ascending order), each line contains three integers p, r, d (p ≤ 100,r ≤ 100 and d ≤ 200) denote processing requirement, release date and due date respectively. |
||||||
Output | ||||||
For each test case, output “Boss xnby is angry!” if no such schedule exists.Otherwise output “Boss xnby is happy!”. |
||||||
Sample Input | ||||||
1
4 3
2 3 5
1 1 4
2 3 7
4 5 9
|
||||||
Sample Output | ||||||
Boss xnby is happy! |
||||||
Hint | ||||||
One schedule: Job 1 must be done in day 3 and day 4, and job 2 can be done in day 1,job 3 can be done in day 5 or day 6,the last job’s first two days can be done in day 5 and day 6(since there 3 machines, so it will not conflict with job 3), the remaining 2 days can be done in day 7 and day 8. |
||||||
Author | ||||||
monster@monster |
题意:
有n项任务m台机器
每项任务可能需要多天来完成
每个机器每天只能处理一个任务 并且每个任务 每天只能每一台机器处理
有n项任务 告诉你每项任务的开始时间,截止时间和完成它所需要的时间
问这些任务能否在规定时间内全部完成?
分析:
这个题是我做的第一个网络流的题
读完题之后我就开始准备建模,
想任务跟机器之间的关系
想了好长时间也没想明白怎么建
后来看周洲的分析恍然大悟
原来我少分析了一个因素:日期
其实只要把任务跟日期之间建图,机器的作用呢,这样来想,每天最多能承受的任务数不就是机器的数量吗?
这样图就建好了
代码:

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 305 << 1; 9 const int INF = 1000000000; 10 11 struct Edge 12 { 13 int from, to, cap, flow; 14 }; 15 16 struct Dinic 17 { 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 25 void ClearAll(int n) { 26 for(int i = 0; i <= n; i++) { 27 G[i].clear(); 28 } 29 edges.clear(); 30 } 31 32 void AddEdge(int from, int to, int cap) { 33 edges.push_back((Edge){from, to, cap, 0} ); 34 edges.push_back((Edge){to, from, 0, 0} ); 35 m = edges.size(); 36 G[from].push_back(m - 2); 37 G[to].push_back(m - 1); 38 //printf("%din end ",m); 39 } 40 41 bool BFS() 42 { 43 memset(vis, 0, sizeof(vis) ); 44 queue<int> Q; 45 Q.push(s); 46 vis[s] = 1; 47 d[s] = 0; 48 while(!Q.empty() ){ 49 int x = Q.front(); Q.pop(); 50 for(int i = 0; i < G[x].size(); i++) { 51 Edge& e = edges[G[x][i]]; 52 if(!vis[e.to] && e.cap > e.flow) { 53 vis[e.to] = 1; 54 d[e.to] = d[x] + 1; 55 Q.push(e.to); 56 } 57 } 58 } 59 return vis[t]; 60 } 61 62 int DFS(int x, int a) { 63 if(x == t || a == 0) return a; 64 int flow = 0, f; 65 for(int& i = cur[x]; i < G[x].size(); i++) { 66 Edge& e = edges[G[x][i]]; 67 if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) { 68 e.flow += f; 69 edges[G[x][i]^1].flow -= f;//这儿wrong了一次 70 flow += f; 71 a -= f; 72 if(a == 0) break; 73 } 74 } 75 return flow; 76 } 77 78 int Maxflow(int s, int t) { 79 this -> s = s; this -> t = t; 80 int flow = 0; 81 while(BFS()) { 82 memset(cur, 0, sizeof(cur) ); 83 flow += DFS(s, INF); 84 } 85 return flow; 86 } 87 }; 88 89 Dinic g; 90 91 int main() 92 { 93 int t; 94 //freopen("a.txt","r",stdin); 95 scanf("%d",&t); 96 int J, M; 97 int p, r, d; 98 while(t--) 99 { 100 scanf("%d %d",&J, &M); 101 int sum = 0; 102 g.ClearAll(405); 103 int min_date = INF, max_date = -INF; 104 for(int i = 1; i <= J; i++) 105 { 106 scanf("%d %d %d",&p, &r, &d); 107 if(min_date > r) 108 min_date = r; 109 if(max_date < d) 110 max_date = d; 111 sum += p; 112 g.AddEdge(0, i, p); 113 for(int j = r; j < d; j++) 114 g.AddEdge(i, j + J, 1); 115 } 116 for(int j = min_date; j <= max_date; j++) 117 { 118 g.AddEdge(j + J, 401, M); 119 } 120 int ans = g.Maxflow(0, 401); 121 // printf("sum:%d ans:%d ",sum, ans); 122 if(ans == sum) 123 puts("Boss xnby is happy!"); 124 else 125 puts("Boss xnby is angry!"); 126 } 127 return 0; 128 }