n Tasks, M machines
Each task has the time to start working, the end time, and the number of days to work on a machine
The work of each task can be broken, only need to work with the machine within the specified time to specify the number of days
On the same day, a task can only be worked by a single machine
Ask if you can schedule time so that all tasks can be completed within the specified time
Create a 1-weighted edge on the task and its work time
In creating a super source point and a super meeting point
The introduction of weights from the source point to the number of days that the task needs to work, from each day to the meeting point to establish a weighted m edge
This way, the maximum flow from the source point to the meeting point is greater than the sum of the number of days that all tasks need to work, and all tasks can be completed at a specified time
Dinic algorithm:
#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace STD;Const intMAXN =10010;Const intINF =0x7fffffff;intSt =0;intEn =1001;intDIS[MAXN];structedge{intV;intNextintW;} EDGE[MAXN*MAXN];intHEAD[MAXN], Nedge;voidAddedge (intU,intVintW) {edge[nedge].v = v; EDGE[NEDGE].W = W; Edge[nedge].next = Head[u]; Head[u] = nedge++; EDGE[NEDGE].V = u; EDGE[NEDGE].W =0; Edge[nedge].next = Head[v]; HEAD[V] = nedge++;}BOOLBFS () { Queue<int>que;memset(Dis,-1,sizeof(DIS)) ; DIS[ST] =0; Que.push (ST); while(Que.size ()) {intU = Que.front (); Que.pop (); for(inti = Head[u]; I! =-1; i = edge[i].next) {intv = edge[i].v;if(Edge[i].w >0&& Dis[v] <0) {Dis[v] = Dis[u] +1; Que.push (v); } } }if(Dis[en] >0)return true;return false;}intDfsintXintMX) {if(x==en)returnmxintIintans=0;intA for(i=head[x];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(dis[v]==dis[x]+1&&edge[i].w>0&& (A=dfs (V,min (MX,EDGE[I].W))) {edge[i].w = A; edge[i^1].W + = A; ans + = A; MX-= A;if(!MX) Break; } }if(!ans) dis[x] =-1;the main optimization of//dinic algorithm returnAns;}intMain () {//freopen ("In.txt", "R", stdin); intt;intCAS =0;intN, M;scanf("%d", &t); while(t--) {scanf("%d%d", &n, &m);intsum =0;memset(Head,-1,sizeof(head)) ; Nedge =0;intMa =0;intMI = inf; for(inti =1; I <= n;i++) {intSi, ei, pi;scanf("%d%d%d", &pi, &si, &ei); Addedge (St, I, PI); sum + = PI; for(intj = si;j <= ei; j + +) Addedge (i, J + -,1) ; } for(intj =501; J <= +; j + +) Addedge (J., en, M);intRes;intAns =0; while(BFS ()) while(res = DFS (ST, INF)) ans + = res;printf("Case%d:", ++cas);if(Ans >= sum)puts("Yes") ;Else puts("No");puts("") ; }return 0;}
ISAP algorithm:
#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace STD;Const intMAXN =10010;Const intINF =0x7fffffff;intSt =0;intEn =1001;intN, M;intDIS[MAXN];intGAP[MAXN], CUR[MAXN],PRE[MAXN];structedge{intV;intNextintW;} EDGE[MAXN*MAXN];intHEAD[MAXN], Nedge;voidAddedge (intU,intVintW) {edge[nedge].v = v; EDGE[NEDGE].W = W; Edge[nedge].next = Head[u]; Head[u] = nedge++; EDGE[NEDGE].V = u; EDGE[NEDGE].W =0; Edge[nedge].next = Head[v]; HEAD[V] = nedge++;}voidBFS () {memset(Dis,-1,sizeof(DIS)) ;memset(Gap,0,sizeof(GAP)) ; Queue<int>que; Dis[en] =0; gap[0]++ ; Que.push (en); while(Que.size ()) {intU = Que.front (); Que.pop (); for(inti = Head[u]; I! =-1; i = edge[i].next) {intv = edge[i].v;if(Dis[v] <0&& edge[i^1].W >0) {Dis[v] = Dis[u] +1; gap[dis[v]]++; Que.push (v); } } }}intIsap () {BFS ();memset(Pre,-1,sizeof(pre)) ;memcpy(Cur, head,sizeof(head)) ;intU = pre[st] = st; gap[0] = N;intMaxflow =0;intThe "." = INF;BOOLFlag; while(Dis[st] < n) {flag =false; for(inti = Cur[u]; I! =-1; i = edge[i].next) {intv = edge[i].v;if(Dis[u] = = Dis[v] +1&& EDGE[I].W >0) {flag =true; Cur[u] = i; PRE[V] = u; U = V; EDGE[I].W = min (----);if(v = = en) {Maxflow + =; for(U = pre[v];; u = pre[u]) {EDGE[CUR[U]].W-=; edge[cur[u]^1].W + =;if(U = = st) Break; } = INF; } Break; } }if(flag)Continue;intMI = n; for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(Dis[v] < mi && EDGE[I].W >0) {Mi=dis[v]; Cur[u]=i; } }if((--gap[dis[u]) = =0) Break; Dis[u] = mi +1; gap[dis[u]]++; U = pre[u]; }returnMaxflow;}intMain () {//Freopen ("In.txt", "R", stdin); intt;intCAS =0;scanf("%d", &t); while(t--) {scanf("%d%d", &n, &m);intsum =0;memset(Head,-1,sizeof(head)) ; Nedge =0;intMa =0;intMI = inf; for(inti =1; I <= n;i++) {intSi, ei, pi;scanf("%d%d%d", &pi, &si, &ei); Addedge (St, I, PI); sum + = PI; for(intj = si;j <= ei; j + +) Addedge (i, J + -,1) ; } for(intj =501; J <= +; j + +) Addedge (J., en, M);intRes;intAns =0; n = n +2+ -; ans = ISAP ();printf("Case%d:", ++cas);if(Ans >= sum)puts("Yes") ;Else puts("No");puts("") ; }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu3572task Schedule Maximum Flow