The maximum stream Dinic algorithm of hdu 3572 Task Schedule, card time .. Creating images is exquisite.

Source: Internet
Author: User

The maximum stream Dinic algorithm of hdu 3572 Task Schedule, card time .. Creating images is exquisite.
Task ScheduleTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 4617 Accepted Submission (s): 1513



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.

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.

Sample Input

24 31 3 5 1 1 42 3 73 5 92 22 1 31 2 2

Sample Output
Case 1: Yes   Case 2: Yes
Maximum stream. The creation of this question is classic, because each machine can only process one task at a time, so the time point can be allocated to each valid machine... specifically, set a super source point S to connect to each task. The capacity is the time required for the task and the time points in which each task is connected, the capacity is 1 (the condition of xxx at each time point is guaranteed), and all time points are connected to the super settlement point T. The capacity is the number of machines, and the maximum flow is obtained, the "yes" code equals the time required for all machines:
# Include
 
  
# Include
  
   
# Include
   
    
# Define SIZE 1100 # define INF 1000000000 using namespace std; struct Edge {int to, w, next;} edge [SIZE * SIZE]; int n, m, start, ends, index = 0; int head [SIZE], level [SIZE], cur [SIZE]; bool visited [SIZE]; bool bfs () {queue
    
     
Que; que. push (start); memset (level,-1, sizeof (level); level [start] = 0; while (! Que. empty () {int pos = que. front (); que. pop (); for (int next = head [pos]; next! =-1; next = edge [next]. next) {if (level [edge [next]. to] <0 & edge [next]. w> 0) {level [edge [next]. to] = level [pos] + 1; que. push (edge [next]. to) ;}} return level [ends]! =-1;} int min (int a, int B) {return a> B? B: a;} int dfs (int pos, int flow) {int deta = 0, tmp = 0; if (pos = ends) return flow; for (int next = head [pos]; next! =-1; next = edge [next]. next) {if (edge [next]. w> 0 & level [pos] = level [edge [next]. to]-1) {tmp = dfs (edge [next]. to, min (flow-deta, edge [next]. w); if (tmp> 0) {edge [next]. w-= tmp; edge [next ^ 1]. w + = tmp; deta + = tmp; if (deta = flow) break;} elselevel [edge [next]. to] =-1; // if this parameter is not added, the request times out...} Return deta;} int dinic () {int ans = 0, flow = 0; while (bfs () {int deta = 0; ans + = dfs (0, INF);} return ans;} void add (int s, int d, int w) {edge [index]. next = head [s]; edge [index]. to = d; edge [index]. w = w; head [s] = index ++; edge [index]. next = head [d]; edge [index]. to = s; edge [index]. w = 0; head [d] = index ++;} int main () {int t, c = 1; scanf ("% d", & t ); while (t --) {scanf ("% d", & n, & m); memset (Head,-1, sizeof (head); index = 0; int sum = 0, max =-1; start = 0; for (int I = 1; I <= n; ++ I) {int x, y, z; scanf ("% d", & x, & y, & z ); sum + = x; max = max> z? Max: z; add (start, I, x); for (int j = y; j <= z; ++ j) {add (I, j + n, 1) ;}} ends = n + max + 1; for (int I = 1; I <= max; ++ I) add (I + n, ends, m ); int ans = dinic (); printf ("Case % d:", c ++); if (ans! = Sum) puts ("No \ n"); elseputs ("Yes \ n");} return 0 ;}
    
   
  
 

Share with Jun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.