Map: Multi-source and multi-sink problem, large petition algorithm to add hyper-sinks and hyper-sources through the body
Think of each task and every day as a point, adding source and sink points.
The source point is connected to each task with an edge, and the capacity is the number of times required to complete the task.
If the I task can be processed from Si to EI days, the task will be connected to these days by an edge, with a capacity of 1, indicating that the task can only be processed once a day.
Finally, from one side of each day to the edge of the meeting point, the capacity is the number of machines m, which means that I can handle m tasks per day.
If the maximum flow is equal to the sum of the number of times that all tasks need to be processed, the task can be completed;
Dinic algorithm
Note: When initializing, you may forget the edges of the 0 connection
#include <vector>#include <cstring>#include <algorithm>#include <cstdio>#include <queue>using namespace STD;#define MAXN 1100#define INF 0x7f7f7f7fstructEdge {intFrom,to,cap,flow;};structDinic {intN,m,s,t; vector<Edge>Edges vector<int>G[MAXN];BOOLVIS[MAXN];intD[MAXN];intCUR[MAXN];voidAddedge (intFromintTo,intCAP) {Edges.push_back (Edge) {from,to,cap,0}); Edges.push_back (Edge) {to,from,0,0}); M=edges.size (); G[from].push_back (M-2); G[to].push_back (M-1); }voidInit () {edges.clear (); for(intI=0; i<=maxn;i++) g[i].clear (); }BOOLBfs () {memset(Vis,0,sizeof(VIS)); Queue<int>Q; while(! Q.empty ()) Q.pop (); Q.push (s); d[s]=0; Vis[s] =1; while(! Q.empty ()) {intx = Q.front (); Q.pop (); for(intI=0; I<g[x].size (); i++) {edge& e = edges[g[x][i]];if(!vis[e.to]&&e.cap>e.flow) {Vis[e.to] =1; D[e.to] = d[x]+1; Q.push (e.to); } } }returnVIS[T]; }intDfsintXintA) {if(x==t| | a==0)returnAintFlow =0, F; for(int&i=cur[x]; I<g[x].size (); i++) {edge& e = edges[g[x][i]];if(d[x]+1==d[e.to]&& (F=dfs (E.to,min (a,e.cap-e.flow)) >0) {E.flow + = f; edges[g[x][i]^1].flow-= f; Flow + + F; A-= f;if(a==0) Break; } }returnFlow }intMaxflow (intSintT) { This->s=s; This->t=t;intFlow =0; while(Bfs ()) {memset(cur,0,sizeof(cur)); Flow+=dfs (S,inf); }returnFlow }};intMain () {intTintN,k; Dinic Slove;intCAS =0;scanf("%d", &t); while(t--) {scanf("%d%d", &n,&k); Slove.init ();intsum =0;intMX =-1; for(intI=1; i<=n;i++) {intX, Y, Zscanf("%d%d%d", &x,&y,&z); Sum+=x; Slove.addedge (0, i,x);if(mx<z) mx = z; for(intj=y;j<=z;j++) {Slove.addedge (i,n+j,1); } }intss=0, tt=mx+n+1; for(intj=1; j<=mx;j++) {Slove.addedge (j+n,tt,k); }printf("Case%d:", ++cas);printf(Slove.maxflow (SS,TT) ==sum?"yes\n\n":"no\n\n"); }}
Copyright notice: All brothers, please feel free to reprint, please indicate who is the brother
HDU 3572 Task Schedule (Multi-source multi-sinks)