zoukankan      html  css  js  c++  java
  • HDU2389(KB10-F 二分图最大匹配Hopcroft_Karp)

    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;
    }
  • 相关阅读:
    数据结构考研--线性表例2-4
    2011 BENELLI TRE1130K所有系统诊断:OBDSTAR MS80或OBDSTAR MS50?
    BMW E60可以通过Autel IM508和XP400 Pro读取数据并学习密钥吗?
    Launch X431 V V4.0 2021最新升级:32GB存储+ 30特殊功能
    Autel IM608 Pro为BMW 2010 535i添加新钥匙
    Xhorse VVDI Prog V5.0.0软件免费下载
    使用XP400 Pro通过Autel IM508读取Benz W207 EIS数据
    由CGDI Prog BMW与GODIAG GT100进行的BMW FEM / BDC密钥编程
    BMW CAS4 / CAS4 +编程测试平台购买建议
    2021 GODIAG GT100 ECU接线盒评测:出色的诊断和节省维修成本的设备
  • 原文地址:https://www.cnblogs.com/Penn000/p/7435772.html
Copyright © 2011-2022 走看看