码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2389(KB10-F 二分图最大匹配Hopcroft_Karp)

时间:2017-08-26 18:25:22      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:ica   cas   ogr   mode   mod   progress   bad   匈牙利   匹配   

Rain on your Parade

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


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

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

 
匈牙利算法 复杂度:V×E, 本题TLE
转换为网络流模型,跑dinic? 本题MLE
所以,只能用Hopcroft_Karp。复杂度:sqrt(V)×E
V为点数,E为边数。
//2017-08-26
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 100000;
const int M = 20000000;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
    int to, next;
}edge[M];


void add_edge(int u, int v){
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

//xlink[i]表示左集合顶点i匹配的右集合的点,ylink[i]表示右集合顶点i匹配的左集合的点
int xlink[N], ylink[N];
//xlevel[i]表示左集合顶点i的所在层数,ylevel[i]表示右集合顶点i的所在层数
int xlevel[N], ylevel[N];
bool vis[N];
struct Hopcroft_Karp{
    int dis, xn, yn;//xn表示左集合顶点个数,yn表示右集合顶点个数
    void init(int _xn, int _yn){
        tot = 0;
        xn = _xn;
        yn = _yn;
        memset(head, -1, sizeof(head));
        memset(xlink, -1, sizeof(xlink));
        memset(ylink, -1, sizeof(ylink));
    }
    bool bfs(){
        queue<int> que;
        dis = INF;
        memset(xlevel, -1, sizeof(xlevel));
        memset(ylevel, -1, sizeof(ylevel));
        for(int i = 0; i < xn; i++)
            if(xlink[i] == -1){
                que.push(i);
                xlevel[i] = 0;
            }
        while(!que.empty()){
            int u = que.front();
            que.pop();
            if(xlevel[u] > dis)break;
            for(int i = head[u]; i != -1; i = edge[i].next){
                int v = edge[i].to;
                if(ylevel[v] == -1){
                    ylevel[v] = xlevel[u] + 1;
                    if(ylink[v] == -1)
                          dis = ylevel[v];
                    else{
                        xlevel[ylink[v]] = ylevel[v]+1;
                        que.push(ylink[v]);
                    }
                }
            }
        }
        return dis != INF;
    }
    int dfs(int u){
        for(int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            if(!vis[v] && ylevel[v] == xlevel[u]+1){
                vis[v] = 1;
                if(ylink[v] != -1 && ylevel[v] == dis)
                      continue;
                if(ylink[v] == -1 || dfs(ylink[v])){
                    xlink[u] = v;
                    ylink[v] = u;
                    return 1;
                }
            }
        }
        return 0;
    }
    //二分图最大匹配
    //input:建好的二分图
    //output:ans 最大匹配数
    int max_match(){
        int ans = 0;
        while(bfs()){
            memset(vis, 0, sizeof(vis));
            for(int i = 0; i < xn; i++)
                  if(xlink[i] == -1)
                      ans += dfs(i);
        }
        return ans;
    }
}hk_match;

int n, m, pour_time;
struct Guests{
    int x, y, speed;
}guests[N];

struct Umbrella{
    int x, y;
}umbrella[N];

bool getUmbrella(int i, int j){
    return (guests[i].x-umbrella[j].x)*(guests[i].x-umbrella[j].x)
        + (guests[i].y-umbrella[j].y)*(guests[i].y-umbrella[j].y)
        <= guests[i].speed*guests[i].speed*pour_time*pour_time;
}

int main()
{
    std::ios::sync_with_stdio(false);
    //freopen("inputF.txt", "r", stdin);
    int T, kase = 0;
    cin>>T;
    while(T--){
        cin>>pour_time>>m;
        for(int i = 0; i < m; i++)
          cin>>guests[i].x>>guests[i].y>>guests[i].speed;
        cin>>n;
        for(int i = 0; i < n; i++)
          cin>>umbrella[i].x>>umbrella[i].y;
        hk_match.init(m, n);
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(getUmbrella(i, j))
                    add_edge(i, j);
        cout<<"Scenario #"<<++kase<<":"<<endl<<hk_match.max_match()<<endl<<endl;
    }

    return 0;
}

 

HDU2389(KB10-F 二分图最大匹配Hopcroft_Karp)

标签:ica   cas   ogr   mode   mod   progress   bad   匈牙利   匹配   

原文地址:http://www.cnblogs.com/Penn000/p/7435772.html

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