Binary graph (h-k algorithm)

Source: Internet
Author: User
Tags cas

Problem Descriptionyou ' re giving a party in the garden of your villa by the sea. The party is a huge success, and everyone are 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 could is 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 It going to rain heavily in just a few minutes! " Your guests all wear their best dresses and really would does 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 is small, and since your guests is all slightly snobbish, no guest would share an umbrella with other guest S. The umbrellas is spread across your (gigantic) garden and just like your guests. To complicate matters even more, some of your guests can ' t run as fast as the others.
Can your guests so as many as possible find a 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, F IND out what many of your guests can at the most reach an umbrella. The want to share a umbrella, however. Guests do not.

Inputthe input starts with a line containing a single integer, the number of the test cases.
Each test case starts with a line containing the time t in minutes until it would start to rain (1 <=t <= 5). The next line contains the number of guests m (1 <= m <= in), followed by M lines containing X-and y-coordinates As well as the speed Si in units per minute (1 <= si <=) of the A guest as integers, separated by spaces. After the guests, a single line contains n (1 <= n <=), 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 was less than 10000.

Outputfor Each test case, write a line containing "Scenario #i:", where I am the number of the the test case starting at 1. Then, write a single line that contains the number of guests that can at the very 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
Test Instructions: Find out the relationship between the person and the umbrella will have to n^2 time, the Hungarian algorithm decisively timed out. h-k algorithm Complexity O (sqrt (n) E).
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string > #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack> #include <set>using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define REP (i, N) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) typedef long long Ll;typedef pair< int,int>pil;const int maxn=3000+100;int head[maxn];int mx[maxn],my[maxn];int dx[maxn],dy[maxn];int USED[MAXN];   struct node{int x, y; int s;} e[maxn];struct node{int x, y;}    pos[maxn];struct edge{int u,v; int next;}    Ee[maxn*maxn];int Cas,t,n,m,cnt,num,dis;bool searchp () {queue<int>q;    dis=0x3f3f3f3f;    CLEAR (dx,-1);    CLEAR (dy,-1);          REPF (I,1,n) if (mx[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=ee[i].next) {int v=ee[i].v;                if (dy[v]==-1) {dy[v]=dx[u]+1;                if (my[v]==-1) dis=dy[v];                    else {dx[my[v]]=dy[v]+1;                Q.push (My[v]); }}}} return dis!=0x3f3f3f3f;}        bool Dfs (int u) {for (int i=head[u];i!=-1;i=ee[i].next) {int v=ee[i].v;            if (!used[v]&&dy[v]==dx[u]+1) {used[v]=1;            if (My[v]!=-1&&dy[v]==dis) continue; if (my[v]==-1| |                DFS (My[v])) {my[v]=u;                Mx[u]=v;            return true; }}} return false;}    void work () {int res=0;    CLEAR (mx,-1);    CLEAR (my,-1);        while (SEARCHP ()) {CLEAR (used,0);    REPF (I,1,n) if (Mx[i]==-1&&dfs (i)) res++; } printf ("Scenario #%d:\n%d\n\n", ++num,res);} bool OK (int i,Int j) {double d=sqrt (double) (e[i].x-pos[j].x) * (e[i].x-pos[j].x) + (double) (E[I].Y-POS[J].Y) * (E[I].Y-POS[J].Y)); return d/e[i].s<=t;}    void Addedge (int u,int v) {ee[cnt].u=u;ee[cnt].v=v;    Ee[cnt].next=head[u]; head[u]=cnt++;}    int main () {int x,y,w;    num=0;    scanf ("%d", &cas);        while (cas--) {CLEAR (head,-1); cnt=0;        scanf ("%d%d", &t,&n);        REPF (i,1,n) scanf ("%d%d%d", &AMP;E[I].X,&AMP;E[I].Y,&AMP;E[I].S);        scanf ("%d", &m);            REPF (i,1,m) {scanf ("%d%d", &pos[i].x,&pos[i].y);        REPF (J,1,n) if (ok (j,i)) Addedge (j,i);    } work (); } return 0;}


Binary graph (h-k algorithm)

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.