HDU2389 Rain on your Parade(HK算法)

Rain on your Parade

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


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
 
先给大神的博客(讲解): 点击打开链接
记录下模板吧,我看懂花了好久的时间。代码其实就是大神博客的代码,我只是加了一点点自己的理解
题意:每个样例第一行 代表还有多少单位时间开始下雨,然后是 n个人,接下来n行是每个人的位置(一维坐标平面内)和他的移动速度,接下来m行 代表雨伞数目,接下来m行表示各个雨伞的位置,问在下雨前最多有多少人能够拿到雨伞(两个人不能共用一把伞)。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXN=3005;//最大点数
const int INF=0x3f3f3f3f;//距离初始值
int Map[MAXN][MAXN];//二分图
int cx[MAXN];//cx[i]表示左集合i顶点所匹配的右集合的顶点序号
int cy[MAXN];//cy[i]表示右集合i顶点所匹配的左集合的顶点序号
int n,m,dis;
int dx[MAXN],dy[MAXN];//dx[i]表示左集合i顶点所在层数
bool vis[MAXN];//dy[i]表示在有集合i顶点所在的层数
int xp[3005],yp[3005],s[3005];
int xu[3005],yu[3005];
bool searchpath(){
    queue<int>Q;
    dis=INF;
    memset(dx,-1,sizeof(dx));
    memset(dy,-1,sizeof(dy));
    for(int i=1;i<=n;++i){
        //cx[i]表示左集合i顶点所匹配的右集合的顶点序号
        if(cx[i]==-1){
            //将未遍历的节点入队并初始化次节点距离为0
            Q.push(i);
            dx[i]=0;
        }
    }
    //广度搜索增广路径
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        if(dx[u]>dis)break;//找到新的增广路就跳出
        //取右侧节点
        for(int i=1;i<=m;++i){
            //右侧节点的增广路径的距离
            if(Map[u][i]&&dy[i]==-1){
                dy[i]=dx[u]+1;//v对应的距离为u对应距离加1
                if(cy[i]==-1)dis=dy[i];//找到了一条增广路
                else{
                    dx[cy[i]]=dy[i]+1;
                    Q.push(cy[i]);
                }
            }
        }
    }
    return dis!=INF;
}

//寻找路径深度搜索
int findpath(int s){
    for(int i=1;i<=m;++i){
        //如果该点没有被遍历过并且距离为上一节点+1
        if(!vis[i]&&Map[s][i]&&dy[i]==dx[s]+1){
            //对该点染色
            vis[i]=1;
            if(cy[i]!=-1&&dy[i]==dis)continue;//因为是广搜,也可能是左集合里两个同层次的点同时找到了右集合里的同一个点作为新的增广路,那么这个点在被用过一次后,第二次就不能用了
            if(cy[i]==-1||findpath(cy[i])){
                cy[i]=s;cx[s]=i;
                return 1;
            }
        }
    }
    return 0;
}

//得到最大匹配的数目
int MaxMatch(){
    int res=0;
    memset(cx,-1,sizeof(cx));
    memset(cy,-1,sizeof(cy));
    while(searchpath()){
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;++i){
            if(cx[i]==-1)res+=findpath(i);
        }
    }
    return res;
}

int main(){
    int T;
    scanf("%d",&T);
    for(int q=1;q<=T;++q){
        memset(Map,0,sizeof(Map));
        int t;
        scanf("%d",&t);
        scanf("%d",&n);
        for(int i=1;i<=n;++i){
            scanf("%d%d%d",&xp[i],&yp[i],&s[i]);
        }
        scanf("%d",&m);
        for(int i=1;i<=m;++i){
            scanf("%d%d",&xu[i],&yu[i]);
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                if((xp[i]-xu[j])*(ll)(xp[i]-xu[j])+(yp[i]-yu[j])*(ll)(yp[i]-yu[j])<=t*(ll)t*s[i]*s[i]){
                    Map[i][j]=1;
                }
            }
        }
        printf("Scenario #%d:\n%d\n\n",q,MaxMatch());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值