HDU2389-Rain on your Parade(二分图匹配Hopcroft-Karp算法)

来源:互联网 发布:淘宝怎么设置快递模板 编辑:程序博客网 时间:2024/04/29 12:50

Rain on your Parade

                                                                            Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)
                                                                                                       Total Submission(s): 4022    Accepted Submission(s): 1338


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 could 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 would 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 across 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 many 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 many 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
 

Recommend
lcy
 

题意:在一个二维坐标系上有nx个人和ny把伞,每个人都有自己的移动速度,问有多少人可以再tmin内移动到不同的雨伞处(不允许两个人共用一把伞)。

解题思路:二分图,要雨伞和人一一对应,只要满足dist(a[i], a[j])<=v*T的点连一条边即可,典型的匹配问题,数据量较大,nx:3000,ny:3000,极限情况下有9000000条边,很明显,匈牙利算法可能会TLE,为了降低时间复杂度,由每次寻找一条增广路径扩展到寻找多条增广路径,Hopcroft-Karp算法,跟Dinic与连续增广路的关系很相似


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#include <cmath>#include <vector>#include <set>#include <stack>#include <map>#include <climits>#include <functional>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;const int MAXN=3010;const int MAXM=3010*3010;struct node{    double x, y;    double v;} a[MAXN],b[MAXN];int nx,ny;int cnt;int dis;int s[MAXN],nt[MAXM],e[MAXM];int x[MAXN],y[MAXN];int dx[MAXN],dy[MAXN];int vis[MAXN];int bfs(){    queue<int>q;    dis=INF;    memset(dx,-1,sizeof(dx));    memset(dy,-1,sizeof(dy));    for(int i=0; i<nx; i++)    {        if(x[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=s[u]; ~i; i=nt[i])        {            int ee=e[i];            if(dy[ee]==-1)            {                dy[ee]=dx[u]+1;                if(y[ee]==-1) dis=dy[ee];                else                {                    dx[y[ee]]=dy[ee]+1;                    q.push(y[ee]);                }            }        }    }    return dis!=INF;}int Find(int u){    for(int i=s[u]; ~i; i=nt[i])    {        int ee=e[i];        if(!vis[ee]&&dy[ee]==dx[u]+1)        {            vis[ee]=1;            if(y[ee]!=-1&&dy[ee]==dis) continue;            if(y[ee]==-1||Find(y[ee]))            {                x[u]=ee,y[ee]=u;                return 1;            }        }    }    return 0;}int MaxMatch(){    int ans=0;    while(bfs())    {        memset(vis,0,sizeof(vis));        for(int i=0; i<nx; i++)        {            if(x[i]==-1)                ans+=Find(i);        }    }    return ans;}double dist(node a,node b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){    int t,cas=0;    scanf("%d",&t);    while(t--)    {        printf("Scenario #%d:\n", ++cas);        cnt=0;        memset(s,-1,sizeof s);        memset(x,-1,sizeof x);        memset(y,-1,sizeof y);        int time;        scanf("%d",&time);        scanf("%d",&nx);        for(int i=0; i<nx; i++)            scanf("%lf %lf %lf",&a[i].x,&a[i].y,&a[i].v);        scanf("%d",&ny);        for(int i=0; i<ny; i++)            scanf("%lf %lf",&b[i].x,&b[i].y);        for(int i=0; i<nx; i++)        {            for(int j=0; j<ny; j++)            {                double limit=a[i].v*time;                double ss=dist(a[i], b[j]);                if(ss<=limit) nt[cnt]=s[i],s[i]=cnt,e[cnt++]=j;            }        }        int ans=MaxMatch();        printf("%d\n\n",ans);    }    return 0;}

0 0
原创粉丝点击