hdu 3572 Task Schedule
阿新 • • 發佈:2017-09-13
cout stdio.h res geometry stdin i++ rst math.h pro
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. n?p個流量能否全部流入T中。
參考代碼:
Task Schedule
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.
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.
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 題意:有n項任務和m臺機器,給出每個任務的執行時間pi,和開始時間si,以及截止時間ei。每項任務需要在si或si後開始執行,在ei或ei前執行完畢,每個任務可以分段,在任何一臺機器執行,每臺機器在一個時間只能執行一個任務。。 題解:拆點的最大流判斷是否滿流,建圖是重點。考慮每個任務可以在任何一臺機器執行,因此每個任務在規定的時間內就可以分到任何一臺機器(一條機器在一個時間點只執行一個任務),所以將時間進行拆點,添加一個源點S,從源點S向每個任務連邊,因為每個任務的執行時間是pi,所以從超級源點連向每個任務的流是pi,即建邊add_edge (S,i,pi);然後考慮每個任務的執行,一個任務只能同時在一個時間點被工作,即不能同時既在時間點A上加工又在時間點B上加工,,所以每一個任務 i 向時間點[s,e]連一條容量為1的邊; add_edge (i,s--->t,1);最後每一個時間點向T連一條容量為m個邊,說明一個時間點只能最多同時有m個機器在工作。最後判斷是否漫滿流,即判斷從S流出的
#include <stdio.h> #include <algorithm> #include <math.h> #include <string.h> #include <vector> #include <queue> #include <stack> #include <iostream> #define pi acos(-1.0) #define INF 0x3f3f3f3f using namespace std; #define ll long long const int maxm=251000+7; const int maxn=1010; struct edge { int t,w,f,next; }e[2*maxm]; int dis[maxn]; int head[maxn],cnt; void add_edge(int u,int v,int f) { e[cnt].t=v,e[cnt].f=f; e[cnt].next=head[u]; head[u]=cnt++; e[cnt].t=u,e[cnt].f=0; e[cnt].next=head[v]; head[v]=cnt++; } void init() { memset(head,-1,sizeof head); cnt=0; } int bfs(int t) { memset(dis,-1,sizeof dis); queue<int> q; dis[0]=1; q.push(0); while(!q.empty()) { int k=q.front();q.pop(); for(int i=head[k];i!=-1;i=e[i].next) { int v=e[i].t; if(e[i].f&&dis[v]==-1) { dis[v]=dis[k]+1; if(v==t) return 1; q.push(v); } } } return dis[t]!=-1; } int dfs(int s,int t,int f) { if(s==t||!f) return f; int r=0; for (int i=head[s]; i!=-1; i=e[i].next) { int v=e[i].t; if(dis[v]==dis[s]+1&&e[i].f) { int d=dfs(v,t,min(f,e[i].f)); if(d>0) { e[i].f-=d; e[i^1].f+=d; r+=d; f-=d; if(!f) break; } } } if(!r) dis[s]=INF; return r; } int main() { freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin); //ios::sync_with_stdio(false); //freopen("C:\\Users\\Administrator\\Desktop\\b.txt","w",stdout); int T,n,m,Case=0; scanf("%d",&T); while(T--) { int st=INF,et=0,sump=0; scanf("%d%d",&n,&m); init(); for(int i=1;i<=n;i++) { int p,s,e; scanf("%d%d%d",&p,&s,&e); add_edge(0,i,p); sump+=p; st=min(st,s); et=max(et,e); for(int j=s;j<=e;j++) add_edge(i,n+j,1); } int t=n+et-st+2; //cout<<n+st<<" "<<n+et<<" "<<t<<endl; for(int i=st+n;i<=et+n;i++) add_edge(i,t,m); int ans=0,res; while(bfs(t)) { res=dfs(0,t,INF); ans+=res; } //printf("%d\n",ans); printf("Case %d: ",++Case); if(ans==sump) puts("Yes"); else puts("No"); printf("\n"); } return 0; }
hdu 3572 Task Schedule