码迷,mamicode.com
首页 > 编程语言 > 详细

二分图最大匹配hopcroft-karp算法——HDU 2389

时间:2014-12-15 17:22:46      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   ar   color   os   sp   for   

对应HDU题目:点击打开链接


Time Limit: 3000MS   Memory Limit: 165535KB   64bit IO Format: %I64d & %I64u

 Status

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



题意:在一个二维坐标系上有n个人和m把伞,每个人都有自己的移动速度,问最多有多少人可以再t min内移动到不同的雨伞处(不允许两个人共用一把伞)。

思路:很明显是二分图的最大匹配问题,用普通DFS,BFS匈牙利算法妥当TLE,之后学了Hopcroft-Karp算法后回来再做;

有点奇怪的是用邻接矩阵无论是效率或空间都比邻接表或vector要好。。。

贴上邻接表代码:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define ms(x,y) memset(x,y,sizeof(x))
const int MAXN=3000+10;
const int MAX=9000000+10;
const int INF=1<<30;
using namespace std;
int time,n,m;
int speed[MAXN];
int head[MAXN];
int vis[MAXN];
int dx[MAXN];
int dy[MAXN];
int link_l[MAXN];
int link_r[MAXN];
int dis;

struct Edge
{
    int v,next;
}E[MAX];

struct COOR
{
    int x,y;
}N[MAXN], M[MAXN];

bool Search()
{
    queue<int>q;
    dis = INF;
    ms(dx, -1);
    ms(dy, -1);
    for(int i = 0; i<n; i++)
        if(link_r[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 = head[u]; i != -1; i = E[i].next){
            int v = E[i].v;
            if(dy[v] == -1){//v是未匹配点
                dy[v] = dx[u] + 1;
                if(link_l[v] == -1) dis = dy[v];//找到一条最小增广路
                else{
                    dx[link_l[v]] = dy[v] + 1;
                    q.push(link_l[v]);
                }
            }
        }
    }
    return dis != INF;
}

int dfs(int u)
{
    for(int i=head[u]; i != -1; i = E[i].next){
        int v = E[i].v;
        if(!vis[v] && dy[v] == dx[u] + 1){
            vis[v] = 1;
            if(link_l[v] != -1 && dy[v] == dis) continue;//层次(也就是增广路径的长度)大于本次查找的dis,是searchP被break的情况,也就是还不确定是否是增广路径,只有等再次调用searchP()在判断。  
            if(link_l[v] == -1 || dfs(link_l[v])){
                link_l[v] = u;
                link_r[u] = v;
                return 1;
            }
        }
    }
    return 0;
}

int MaxMatch()
{
    int res = 0;
    ms(link_l, -1);
    ms(link_r, -1);
    while(Search())
    {
        ms(vis, 0);
        for(int i=0; i<n; i++)
            if(link_r[i] == -1) res += dfs(i);
    }
    return res;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int T, w = 0;;
    scanf("%d", &T);
    while(T--)
    {
        ms(head, -1);
        int size = 0;
        scanf("%d%d", &time, &n);
        for(int i=0; i<n; i++)
            scanf("%d%d%d", &N[i].x, &N[i].y, &speed[i]);
        scanf("%d", &m);
        for(int i=0; i<m; i++)
            scanf("%d%d", &M[i].x, &M[i].y);
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if((N[i].x - M[j].x) * (N[i].x - M[j].x) + (N[i].y - M[j].y) * (N[i].y - M[j].y) <= time * speed[i] * time * speed[i]){
                    E[size].v = j;
                    E[size].next = head[i];
                    head[i] = size++;
                }
            }
        }
        printf("Scenario #%d:\n%d\n\n", ++w, MaxMatch());
    }
    return 0;
}







二分图最大匹配hopcroft-karp算法——HDU 2389

标签:des   style   http   io   ar   color   os   sp   for   

原文地址:http://blog.csdn.net/u013351484/article/details/41944745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!