HDU 2389 Rain on your Parade //MAXMATCH

Rain on your Parade

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


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 <= s i  <= 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
  
  
2 1 2 1 0 3 3 0 3 2 4 0 6 0 1 2 1 1 2 3 3 2 2 2 2 4 4
 

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

Source
 

Recommend
lcy
 
悲剧的二分图匹配,我还是想着尽可能减少时间,各种改还是过不去,
只能学HK算法了,希望那个给偶力量A了这题
#include<stdio.h>
#include<string.h>
#include<math.h>
int n,m;
struct guess
{
    int x,y;
}g[3001],u[3001];
int s[3001],link[3001];
int mat[3001][3001];
bool used[3001];
double check(guess a,guess b)
{
    double tt=1.0;
    return sqrt( tt*(a.x-b.x)*(a.x-b.x)+tt*(a.y-b.y)*(a.y-b.y) );
}
bool can(int t)
{
    for(int i=1;i<=mat[t][0];i++)
    {
      int y=mat[t][i];
      if(used[y]==false)
      {
        used[y]=true;
        if(link[y]==-1||can(link[y]))
        {
            link[y]=t;
            return true;
        }
      }
    }
    return false;
}
int MaxMatch()
{
    int num=0;
    memset(link,-1,sizeof(link));
    for(int i=1;i<=n;i++)
    {
        memset(used,false,sizeof(used));
        if(can(i))  num++;
    }
    return num;
}
int main()
{
    int T,time;
    scanf("%d",&T);
    int cas=1;
    while(T--)
    {
        for(int i=1;i<=n;i++) mat[i][0]=0;
        scanf("%d",&time);
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d%d%d",&g[i].x,&g[i].y,&s[i]);
        scanf("%d",&m);
        for(int i=1;i<=m;i++) scanf("%d%d",&u[i].x,&u[i].y);
        for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
              double l=check(g[i],u[j]);
              if(l<=time*s[i])  mat[i][++mat[i][0]]=j;
          }
        printf("Scenario #%d:/n",cas++);
        printf("%d/n",MaxMatch());
        printf("/n");
    }
    return 0;
}
稍后给出HK
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
int distx[3001],disty[3001],matex[3001],matey[3001],q[3001];
vector<int> g[3001];
struct guess
{
    int x,y;
}gg[3001],u[3001];
int s[3001],n,m;
double check(guess a,guess b)
{
    double tt=1.0;
    return sqrt( tt*(a.x-b.x)*(a.x-b.x)+tt*(a.y-b.y)*(a.y-b.y) );
}
bool BFS(int n)
{
    bool found=false;
    int fore=0,rear=0;
    for(int i=0;i<n;++i)
    {
        if(matex[i]==-1) q[rear++]=i;//加入队列
        distx[i]=0;//要匹配的一边
        disty[i]=0;//要匹配的另一边
    }
    for(;fore<rear;++fore)//队列不空
    {
        int x=q[fore];//从队列从取出x,并扩展
        for(vector<int>::const_iterator it=g[x].begin();it!=g[x].end();++it)
        {
            if (!disty[*it])//选择未访问的点,因为是要求不相交的增广路径
            {
                disty[*it]=distx[x]+1;//至少是1,
                if(matey[*it]==-1)//为被匹配的自由点,未被访问过,说明找到一条路径
                    found=true;
                else
                {
                    distx[matey[*it]]=disty[*it]+1;
                    q[rear++]=matey[*it];
                }
            }
        }
    }
    return found;
}
bool DFS(int x)
{
    for (vector<int>::const_iterator it = g[x].begin(); it != g[x].end(); ++it)
        if (disty[*it] == distx[x]+1)
        {//走了这条边,但是不一定是增广路径
            disty[*it] = 0;
            if (matey[*it] == -1 || DFS(matey[*it]))
                return matex[x] = *it, matey[*it] = x, true;
        }
    return false;
}
int Hopcroft_Karp()
{
    int res=0;
    memset(matex,-1,sizeof(matex));
    memset(matey,-1,sizeof(matey));
    while(BFS(n))
        for(int i=0;i<n;++i)//可能存在多个增广路径
            if(matex[i]==-1&&DFS(i))//找到了一个增广路径
                ++res;
    return res;
}
int main()
{
    int T,time;
    scanf("%d",&T);
    int cas=1;
    while(T--)
    {
        scanf("%d",&time);
        scanf("%d",&n);
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=0;i<n;i++) scanf("%d%d%d",&gg[i].x,&gg[i].y,&s[i]);
        scanf("%d",&m);
        for(int i=0;i<m;i++) scanf("%d%d",&u[i].x,&u[i].y);
        for(int i=0;i<n;i++)
          for(int j=0;j<m;j++)
          {
              double l=check(gg[i],u[j]);
              if(l<=time*s[i])  g[i].push_back(j);
          }
        printf("Scenario #%d:/n",cas++);
        printf("%d/n",Hopcroft_Karp());
        printf("/n");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值