HDU 2389 Rain on your Parade(二分图匹配的hopcroft-carp算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2389

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个人,m把伞,距离下雨还有t的时间,以下给出n个人的坐标和速度,m把伞的坐标,一把伞只能一个人撑,让最多的人不被淋雨。显然的二分图最大匹配,上去就是匈牙利算法,成功TLE。百度了一下,原来还有更优的hopcroft-carp算法,用这个算法就ok了

算法讲解链接:二分图匹配模板(匈牙利算法、hopcroft-carp算法)

代码:

/*#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 3005;

int n,m;
int t;
int used[N];
int Next[N];
struct node1
{
    int x,y;
    int speed;
}peo[N];

struct node2
{
    int x,y;
}umb[N];

ll dis(int i,int j)
{
    return (ll)((peo[i].x-umb[j].x)*(peo[i].x-umb[j].x)+(peo[i].y-umb[j].y)*(peo[i].y-umb[j].y));
}

bool check(int i,int j)
{
    if(dis(i,j)<=(ll)t*t*peo[i].speed*peo[i].speed)
        return true;
    return false;
}

bool Find(int x)
{
    //cout<<"enter2..."<<endl;
    for(int i=1;i<=m;i++)
    {
        if(check(x,i)&&!used[i])  //如果我能跑到
        {
            used[i] = 1;
            if(Next[i]==0||Find(Next[i]))
            {
                Next[i] = x;
                return true;
            }
        }
    }
    return false;
}

int maxMatch()
{
    //cout<<"enter1..."<<endl;
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        MEM(used,0);
        if(Find(i))
            sum++;
    }
    return sum;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int T;
    scanf("%d",&T);
    int cnt=0;
    while(T--)
    {
        cnt++;
        MEM(Next,0);
        scanf("%d",&t);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d%d",&peo[i].x,&peo[i].y,&peo[i].speed);

        scanf("%d",&m);
        for(int i=1;i<=m;i++)
            scanf("%d%d",&umb[i].x,&umb[i].y);

        int ans = maxMatch();
        printf("Scenario #%d:\n",cnt);
        printf("%d\n\n",ans);
    }
    return 0;
}
*/

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 3005;

const int MAXN=3005;// 最大点数
int bmap[MAXN][MAXN];//二分图
int cx[MAXN];//cx[i]表示左集合i顶点所匹配的右集合的顶点序号
int cy[MAXN]; //cy[i]表示右集合i顶点所匹配的左集合的顶点序号
int dis;
int n,m;
int dx[MAXN],dy[MAXN];  //dx表示到x的距离,dy表示到y的距离
int used[MAXN];        //在每次增广中是否使用i点
int t;

struct node1
{
    int x,y;
    int speed;
}peo[N];

struct node2
{
    int x,y;
}umb[N];

ll Dis(int i,int j)
{
    return (ll)((peo[i].x-umb[j].x)*(peo[i].x-umb[j].x)+(peo[i].y-umb[j].y)*(peo[i].y-umb[j].y));
}

bool check(int i,int j)
{
    if(Dis(i,j)<=(ll)t*t*peo[i].speed*peo[i].speed)
        return true;
    return false;
}

bool SearchPath()   //bfs寻找增广路集
{
    queue<int>Q;
    dis = INF;  //存每一次增广的距离
    memset(dx,-1,sizeof(dx));
    memset(dy,-1,sizeof(dy));
    for(int i=1;i<=n;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 v=1;v<=m;v++)
        {
            if(check(u,v)&&dy[v]==-1)
            {
                dy[v] = dx[u] + 1;  //v的距离为u的对应距离+1
                if(cy[v]==-1)       //如果该点未匹配,增广路形成
                    dis = dy[v];
                else                //如果该点已匹配,那么接着往下搜
                {
                    dx[cy[v]] = dy[v] + 1;
                    Q.push(cy[v]);
                }
            }
        }
    }
    return dis!=INF;
}

bool DFS(int u)
{
    for(int v=1;v<=m;v++)
    {
        //如果该点没有被使用过,并且距离为上一节点+1
        if(!used[v]&&check(u,v)&&dy[v]==dx[u]+1)
        {
            used[v] = 1;  //标记使用过该点
            //如果该点已经被匹配了并且为最后一个匹配点,那么这条路径不是增广路,即这条路的结点已经匹配
            if(cy[v]!=-1&&dy[v]==dis)
                continue;
            if(cy[v]==-1||DFS(cy[v]))
            {
                cy[v]=u,cx[u]=v;
                return true;
            }
        }
    }
    return false;
}

int MaxMatch()
{
    int sum = 0;
    memset(cx,-1,sizeof(cx));
    memset(cy,-1,sizeof(cy));
    while(SearchPath()) //当存在增广路,继续松弛
    {
        memset(used,0,sizeof(used));   //每一次的右边的点是否用过
        for(int i=1;i<=n;i++)
        {
            if(cx[i]==-1&&DFS(i))    //如果当前这个点没连过,且能找到增宽路
                sum++;
        }
    }
    return sum;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int T;
    scanf("%d",&T);
    int cnt=0;
    while(T--)
    {
        cnt++;
        scanf("%d",&t);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d%d",&peo[i].x,&peo[i].y,&peo[i].speed);

        scanf("%d",&m);
        for(int i=1;i<=m;i++)
            scanf("%d%d",&umb[i].x,&umb[i].y);

        int ans = MaxMatch();
        printf("Scenario #%d:\n",cnt);
        printf("%d\n\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值