PS: the quality of the Multi-school league is still high. No, no. You only need to understand the problem.
Reference blog: http://blog.csdn.net/luyuncheng/article/details/7944417
After reading the problem solution in the above blog, the idea is ready. However, creating a graph is still a little troublesome. Set the Source Vertex to n + 1 (do not want to start from 0 and do not modify the template). The sink vertex is n + 2 + Max, where Max is the maximum value of EI in the question.
I have doubts about this question: the time complexity of the optimized sap algorithm is O (M * n ^ 2). The maximum N value of this question is 1000, M is 0.5 million, and the time exceeds 0.5 billion. The 1 s time limit has passed.
There was an episode, which I thought for a long time. Why is "=" used to determine whether the stream is full "? > = No? Finally, I figured out that the maximum flow value obtained must be <= sum. Because the maximum value of the stream from the source is sum.
The following is the code: You can use a template to create a graph. For more information about the template, see graph theory and application.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int n = 1010, M = 510000, INF = 0x3f3f3f; struct node {int to, next, W;} edge [m]; int head [N], numh [N], H [N], cure [N], pre [N]; // numh: array of statistic heights optimized by gap; H: array of distance labels; cure: Current Arc int ans, TOT; void SAP (INT s, int e, int N) {int flow, U, TMP, neck, I; ans = 0; for (I = 1; I <= N; I ++) cure [I] = head [I]; numh [0] = N; u = s; while (H [s] <n) {If (u = e) {fl Ow = inf; for (I = s; I! = E; I = edge [cure [I]. to) {If (flow> edge [cure [I]. w) {neck = I; flow = edge [cure [I]. W ;}} for (I = s; I! = E; I = edge [cure [I]. to) {TMP = cure [I]; edge [TMP]. w-= flow; edge [TMP ^ 1]. W + = flow;} ans + = flow; u = neck;} for (I = cure [u]; I! =-1; I = edge [I]. next) if (edge [I]. W & H [u] = H [edge [I]. to] + 1) break; if (I! =-1) {cure [u] = I; Pre [edge [I]. to] = u; u = edge [I]. to;} else {If (0 = -- numh [H [u]) break; // gap optimization cure [u] = head [u]; for (TMP = N, I = head [u]; I! =-1; I = edge [I]. next) if (edge [I]. w) TMP = min (TMP, H [edge [I]. to]); H [u] = TMP + 1; ++ numh [H [u]; If (u! = S) u = pre [u] ;}} void Init () {tot = 0; memset (Head,-1, sizeof (head); memset (PRE, -1, sizeof (pre); memset (H, 0, sizeof (h); memset (numh, 0, sizeof (numh);} void addedge (int I, int J, int W) {edge [tot]. to = J; edge [tot]. W = W; edge [tot]. next = head [I]; head [I] = tot ++; edge [tot]. to = I; edge [tot]. W = 0; edge [tot]. next = head [J]; head [J] = tot ++;} int main () {// freopen ("test.txt", "r", stdin); int N, m, I, J, K, Cas, T = 1, A, B, C, Max, sum, s; scanf ("% d", & CAS ); while (CAS --) {scanf ("% d", & N, & M); Init (); max = 0; sum = 0; S = n + 1; for (I = 1; I <= N; I ++) {scanf ("% d", & A, & B, & C ); sum + = A; max = max (max, c); addedge (S, I, a); For (j = B; j <= C; j ++) addedge (I, j + S, 1);} k = S + 1 + Max; for (I = 1; I <= max; I ++) addedge (S + I, K, M); SAP (S, K, k); printf ("case % d:", t ++ ); if (ANS = sum) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}
View code
The maximum stream of hdu3572task schedule is used to determine the sap Algorithm for full stream optimization.