HDU 2389 Rain on your Parade (Bipartite Graph Matching (Hopcroft-Carp algorithm template ))

Source: Internet
Author: User

HDU 2389 Rain on your Parade (Bipartite Graph Matching (Hopcroft-Carp algorithm template ))
Rain on your ParadeTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Others)
Total Submission (s): 3033 Accepted Submission (s): 952



Problem Description You're giving a party in the garden of your villa by the sea. the party is a huge success, and everyone is here. it's a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. the evening is progressing just as you had imagined. it cocould be the perfect end of a beautiful day.
But nothing ever is perfect. One of your guests works in weather forecasting. He suddenly yells, "I know that breeze! It means its going to rain heavily in just a few minutes !" Your guests all wear their best dresses and really wocould not like to get wet, hence they stand terrified when hearing the bad news.
You have prepared a few umbrellas which can protect a few of your guests. the umbrellas are small, and since your guests are all slightly snobbish, no guest will share an umbrella with other guests. the umbrellas are spread when SS your (gigantic) garden, just like your guests. to complicate matters even more, some of your guests can't run as fast as the others.
Can you help your guests so that as your as possible find an umbrella before it starts to pour?

Given the positions and speeds of all your guests, the positions of the umbrellas, and the time until it starts to rain, find out how much of your guests can at most reach an umbrella. two guests do not want to share an umbrella, however.

Input The input starts with a line containing a single integer, the number of test cases.
Each test case starts with a line containing the time t in minutes until it will start to rain (1 <= t <= 5 ). the next line contains the number of guests m (1 <= m <= 3000 ), followed by m lines containing x-and y-coordinates as well as the speed si in units per minute (1 <= si <= 3000) of the guest as integers, separated by spaces. after the guests, a single line contains n (1 <=n <= 3000), the number of umbrellas, followed by n lines containing the integer coordinates of each umbrella, separated by a space.
The absolute value of all coordinates is less than 10000.

Output For each test case, write a line containing "Scenario # I:", where I is the number of the test case starting at 1. then, write a single line that contains the number of guests that can at most reach an umbrella before it starts to rain. terminate every test case with a blank line.

Sample Input

2121 0 33 0 324 06 0121 1 23 3 222 24 4

Sample Output
Scenario #1:2Scenario #2:2

Source HDU 2008-10 Public Contest, the next N rows represent the location of each visitor (in a one-dimensional coordinate plane) and his moving speed. The next M lines represent the number of umbrellas, and the next M lines represent the location of each umbrella, ask how many people can get an umbrella before the rain (two people cannot share an umbrella ).
Resolution: to build an edge when a person can reach the position of the umbrella within a given period of time, and the maximum matching of the two points, if the classical Hungarian algorithm is used to time out, available Hopcroft-Karp Algorithm (O (sqrt (n) * edgnum )). 546 ms ac.
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; const int N = 3005; const int INF = 1 <28; int head [N], to [N * N], next1 [N * N], tot; // save graph int dx [N], dy [N], dis; // distance between the left and right, record the maximum distance between unmatched vertices in the right part of the record int machx [N], nx, machy [N]; // The number of vertices on the right and left matching the vertices on the left, the right vertex matches the left vertex bool vist [N]; void addEdg (int u, int v) {to [tot] = v; next1 [tot] = head [u]; head [u] = tot ++;} bool searchpath () {// check whether there is an extended queue.
     
      
Q; dis = INF; memset (dx,-1, sizeof (dx); memset (dy,-1, sizeof (dy); for (int I = 1; I <= nx; I ++) if (machx [I] =-1) q. push (I), dx [I] = 0; while (! Q. empty () {int u = q. front (); q. pop (); if (dx [u]> dis) break; for (int I = head [u]; I! =-1; I = next1 [I]) {int v = to [I]; if (dy [v] =-1) {dy [v] = dx [u] + 1; if (machy [v] =-1) dis = dy [v]; else {dx [machy [v] = dy [v] + 1; q. push (machy [v]) ;}}} return dis! = INF;} bool findroad (int u) {for (int I = head [u]; I! =-1; I = next1 [I]) {int v = to [I]; if (! Vist [v] & dy [v] = dx [u] + 1) {vist [v] = 1; if (machy [v]! =-1 & dy [v] = dis) continue; if (machy [v] =-1 | findroad (machy [v]) {machy [v] = u; machx [u] = v; return true ;}}return false;} int MaxMatch () {int ans = 0; memset (machx, -1, sizeof (machx); memset (machy,-1, sizeof (machy); while (searchpath () {memset (vist, 0, sizeof (vist); for (int I = 1; I <= nx; I ++) if (machx [I] =-1) ans + = findroad (I);} return ans;} // ------------- the code above is the template --------------------- struct node {int x, y; double dis;} man [N], umb [N]; double countDis (int u, int v) {return sqrt (man [u]. x-umb [v]. x) * (man [u]. x-umb [v]. x) * 1.0 + (man [u]. y-umb [v]. y) * (man [u]. y-umb [v]. y) * 1.0);} int main () {int T, ny, tim, c = 0; scanf ("% d", & T); while (T --) {scanf ("% d", & tim, & nx); for (int I = 1; I <= nx; I ++) {scanf ("% d % lf", & man [I]. x, & man [I]. y, & man [I]. dis); man [I]. dis * = tim;} scanf ("% d", & ny); for (int I = 1; I <= ny; I ++) scanf ("% d", & umb [I]. x, & umb [I]. y); // --------------------- create graph --------------------- tot = 0; memset (head,-1, sizeof (head); for (int u = 1; u <= nx; u ++) for (int v = 1; v <= ny; v ++) if (man [u]. dis> = countDis (u, v) addEdg (u, v); // ------------------------------------------------ int ans = MaxMatch (); printf ("Scenario # % d: \ n % d \ n ", ++ c, ans);} return 0 ;}
     
    
   
  
 


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.