Hdu 2389 Rain on your Parade hk算法

题目:

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

思路:

此题用匈牙利算法会超时,得用hk算法。

这里先附上匈牙利算法:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn=3*1e3+5;
int q;
int n,m;
int t;
int px[maxn],py[maxn],s[maxn];
int ux[maxn],uy[maxn];
int match[maxn];
int vis[maxn];
struct edge
{
    int to;
    int next;
}edge[maxn*maxn];
int head[maxn];
double dis (int x1,int y1,int x2,int y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void add (int id,int u,int v)
{
    edge[id].to=v;
    edge[id].next=head[u];
    head[u]=id;
}
bool Find(int x)
{
    for (int i=head[x];i!=-1;i=edge[i].next)
    {
        int u=edge[i].to;
        if(!vis[u])
        {
            vis[u]=1;
            if(match[u]==-1||Find(match[u]))
            {
                match[u]=x;
                return true;
            }
        }
    }
    return false;
}
int alor()
{
    int sum=0;
    for (int i=1;i<=n;i++)
    {
        memset (vis,0,sizeof(vis));
        if(Find(i)) sum++;
    }
    return sum;
}
int main()
{
    scanf("%d",&q);
    for (int k=1;k<=q;k++)
    {
        memset (head,-1,sizeof(head));
        memset (match,-1,sizeof(match));
        scanf("%d",&t);
        scanf("%d",&m);
        for (int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&px[i],&py[i],&s[i]);
        }
        scanf("%d",&n);
        for (int i=1,id=0;i<=n;i++)
        {
            scanf("%d%d",&ux[i],&uy[i]);
            for (int j=1;j<=m;j++)
            {
                if(s[j]*t*1.0<dis(px[j],py[j],ux[i],uy[i])) continue;
                add(++id,i,j);
            }
        }
        printf("Scenario #%d:\n",k);
        printf("%d\n\n",alor());
    }
    return 0;
}

然后再是hk算法:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=3*1e3+5;
const int INF=0x3f3f3f3f;
int q;
int n,m;
int dis;
int t;
int px[maxn],py[maxn],s[maxn];
int ux[maxn],uy[maxn];
int match[maxn];
int vis[maxn];
int Mx[maxn],My[maxn];
int dx[maxn],dy[maxn];
struct edge
{
    int to;
    int next;
}edge[maxn*maxn];
int head[maxn];
double distance (int x1,int y1,int x2,int y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void add (int id,int u,int v)
{
    edge[id].to=v;
    edge[id].next=head[u];
    head[u]=id;
}
bool Search()
{
    queue<int>q;
    dis=INF;
    memset (dx,-1,sizeof(dx));
    memset (dy,-1,sizeof(dy));
    for (int i=1;i<=n;i++)
    {
        if(Mx[i]==-1)
        {
            q.push(i);
            dx[i]=0;
        }
    }
    while(!q.empty())
    {
        int now=q.front(); q.pop();
        if(dx[now]>dis) break;
        for (int i=head[now];i!=-1;i=edge[i].next)
        {
            int u=edge[i].to;
            if(dy[u]==-1)
            {
                dy[u]=dx[now]+1;
                if(My[u]==-1)
                {
                    dis=dy[u];
                }
                else
                {
                    dx[My[u]]=dy[u]+1;
                    q.push(My[u]);
                }
            }
        }
    }
    return dis!=INF;
}
bool dfs (int u)
{
    for (int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!vis[v]&&dy[v]==dx[u]+1)
        {
            vis[v]=1;
            if(My[v]!=-1&&dy[v]==dis) continue;
            if(My[v]==-1||dfs(My[v]))
            {
                Mx[u]=v;
                My[v]=u;
                return true;
            }
        }
    }
    return false;
}
int maxMatch ()
{
    int sum=0;
    memset (Mx,-1,sizeof(Mx));
    memset (My,-1,sizeof(My));
    while(Search())
    {
        memset (vis,0,sizeof(vis));
        for (int i=1;i<=n;i++)
        {
            if(Mx[i]==-1&&dfs(i))
            {
                sum++;
            }
        }
    }
    return sum;
}
int main()
{
    scanf("%d",&q);
    for (int k=1;k<=q;k++)
    {
        memset (head,-1,sizeof(head));
        memset (match,-1,sizeof(match));
        scanf("%d",&t);
        scanf("%d",&m);
        for (int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&px[i],&py[i],&s[i]);
        }
        scanf("%d",&n);
        for (int i=1,id=0;i<=n;i++)
        {
            scanf("%d%d",&ux[i],&uy[i]);
            for (int j=1;j<=m;j++)
            {
                if(s[j]*t*1.0<distance(px[j],py[j],ux[i],uy[i])) continue;
                add(++id,i,j);
            }
        }
        printf("Scenario #%d:\n",k);
        printf("%d\n\n",maxMatch());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/88321084