HDU2389 Rain on your Parade(HK算法)

来源:互联网 发布:淘宝怎么不能批量发货 编辑:程序博客网 时间:2024/06/04 18:59

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 <= 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
 
先给大神的博客(讲解):点击打开链接
https://www.cnblogs.com/cenariusxz/p/6592399.html
记录下模板吧,我看懂花了好久的时间。代码其实就是大神博客的代码,我只是加了一点点自己的理解
题意:每个样例第一行 代表还有多少单位时间开始下雨,然后是 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
原创粉丝点击