The problem is very classical, the method is very ingenious, because the completion of each task does not have to be continuous, and can change the machine to complete, and we notice that the time point of up to 500, very small, so we will point out time to build the map.
For each task, the point within its time range is connected to it, the capacity is obviously 1, and is connected to the sink, with a capacity of p[i]. For each point in time, the capacity is m because there can be at most one m machine working at the same time.
At first, I thought about how the task and the machine were connected.
Recommended topic: Click to open link
See the code for details:
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int INF = 100000000;const int maxn = n; int T,cnt,a,b,m,p[maxn],s[maxn],e[maxn],d,kase = 0,v,c,n;struct Edge {int from, to, cap, flow;}; BOOL operator < (const edge& A, const edge& b) {return A.from < B.from | | (A.from = = B.from && a.to < b.to);} struct Dinic {int n, m, S, t; Vector<edge> edges; Twice times the number of sides vector<int> G[MAXN]; adjacency table, G[i][j] indicates the ordinal of the J-side of the node I in the E-array of bool VIS[MAXN]; BFS using int D[MAXN]; Distance from starting point to I int CUR[MAXN]; Current arc pointer void init (int n) {for (int i = 0; i < n; i++) g[i].clear (); Edges.clear ();} void Addedge (int from, int to, int caps) {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);} BOOL BFS () {memset (Vis, 0, sizeof (VIS)); Queue<int> Q; Q.push (s); Vis[s] = 1; D[s] = 0; while (! Q.eMpty ()) {int x = Q.front (); Q.pop (); for (int i = 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); }}} return vis[t];} int DFS (int x, int a) {if (x = = T | | a = = 0) return A; int flow = 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; }} return flow;} int Maxflow (int s, int t) {this->s = s; this->t = t; int flow = 0; while (BFS ()) {memset (cur, 0, sizeof (cur)); Flow + = DFS (s, INF); } return flow; }}g;int Main () {scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); int max_t = 0, flow = 0; for (int i=1;i<=n;i++) {scanf ("%d%d%d", &p[i],&s[i],&e[i]); Flow + = P[i]; max_t = max (Max_t,e[i]); } g.init (max_t+n+5); for (int i=1;i<=max_t;i++) {G.addedge (0,i,m); } for (int i=1;i<=n;i++) {for (int j=s[i];j<=e[i];j++) G.addedge (j,max_t+i,1); G.addedge (Max_t+i,max_t+n+1,p[i]); } if (flow = = G.maxflow (0,max_t+n+1)) printf ("Case%d:yes\n\n", ++kase); else printf ("Case%d:no\n\n", ++kase); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 3572 Task Schedule (maximum flow dinic algorithm)