标签:
Eric 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 available 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 Eric’s company, there are some parallel machines can do these jobs. 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) are allowed. Now Eric 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 exists. Eric is a boss, he is a big shot, and had no time to do with these trivial things, so he arranged for you to do this task.
An integer T (T ≤ 100) indicated the number of test cases. For each test case:
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.
For each test case, output “Boss Eric is angry!” if no such schedule exists. Otherwise output “Boss Eric is happy!”.
1 4 3 2 3 5 1 1 4 2 3 7 4 5 9
Boss Eric is happy!
题目大意:n个任务,在m台机器上完成,其中n给定开始时间及截止时间和工作完成所需时间,问n个任务能否顺利完成;
idea:因为是不确定的一对多的关系,自然想到最大流,任务跟机器进行匹配就好;
总结:开始的时候,日期和机器是分开的点,就导致了机器的点是机器及日期的二维组合,再连边就爆了内存,然后跟学长交流发现建立一维的日期点就好,
机器作为权值,然后就简化的太多了。后来思考下,我的buge是最大流的点太太多的问题,那就是说是不是可以通过点得合并来优化最大流问题,没试过
碰到题了再说;
代码:
#include <bits/stdc++.h> using namespace std; const int maxn=2200; const int inf=100000; struct edge{int to, cap, rev ;}; vector<edge>g[maxn]; int level[maxn]; int iter[maxn]; void add_edge(int from,int to,int cap) { g[from].push_back((edge){to,cap,g[to].size() } ); g[to].push_back((edge){from,0,g[from].size()-1 } ); } void bfs(int s) { memset(level,-1,sizeof(level)); queue<int>que; level[s]=0; que.push(s); while(!que.empty()) { int v=que.front();que.pop(); for(int i=0;i<g[v].size();i++) { edge &e=g[v][i]; if(e.cap>0&&level[e.to]<0){ level[e.to]=level[v]+1; que.push(e.to); } } } } int dfs(int v,int t,int f) { if(v==t)return f; for(int &i=iter[v];i<g[v].size();i++) { edge &e =g[v][i]; if(e.cap>0&&level[v]<level[e.to]) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0){e.cap-=d; g[e.to][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t) { int flow=0; for(;;) { bfs(s); if(level[t]<0)return flow; memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,inf))>0)flow+=f; } } int main() { int t; cin>>t; while(t--) { for(int i=0;i<maxn;i++) g[i].clear(); int n,m; cin>>n>>m; int ans=0; for(int i=0;i<n;i++) { int p,r,d; cin>>p>>r>>d; add_edge(0,200+i,p); ans+=p; for(int j=r;j<d;j++) { add_edge(200+i,j,1); } } for(int i=1;i<200;i++) { add_edge(i,200+n,m); } int bb=max_flow(0,200+n); // cout<<bb<<endl; // cout<<ans<<endl; if(bb==ans)cout<<"Boss Eric is happy!"<<endl; else cout<<"Boss Eric is angry!"<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/aishuijdemiaomiao/p/5794959.html