zoukankan      html  css  js  c++  java
  • HDU2255——KM算法——奔小康赚大钱

    Problem Description
    传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子。
    这可是一件大事,关系到人民的住房问题啊。村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住的话,容易引起不安定因素),每家必须分配到一间房子且只能得到一间房子。
    另一方面,村长和另外的村领导希望得到最大的效益,这样村里的机构才会有钱.由于老百姓都比较富裕,他们都能对每一间房子在他们的经济范围内出一定的价格,比如有3间房子,一家老百姓可以对第一间出10万,对第2间出2万,对第3间出20万.(当然是在他们的经济范围内).现在这个问题就是村领导怎样分配房子才能使收入最大.(村民即使有钱购买一间房子但不一定能买到,要看村领导分配的).
     
    Input
    输入数据包含多组测试用例,每组数据的第一行输入n,表示房子的数量(也是老百姓家的数量),接下来有n行,每行n个数表示第i个村名对第j间房出的价格(n<=300)。
     
    Output
    请对每组数据输出最大的收入值,每组的输出占一行。

     
    Sample Input
    2 100 10 15 23
     
    Sample Output
    123
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2063 1068 3360 1083 2444 
    模板题
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn = 310;
    int n;
    int visx[maxn],visy[maxn],Lx[maxn],Ly[maxn],map[maxn][maxn];
    int match[maxn];
    int find(int u)
    {
        visx[u] = 1;
        for(int v = 1; v <= n ; v++){
            if(!visy[v] && Lx[u] + Ly[v] == map[u][v]){
                visy[v] = 1;
                if(match[v] == -1 || find(match[v])){
                    match[v] = u;
                   return 1;
                }
            }
        }
        return 0;
    }
    void update()
    {
        int a = 1 << 30;
        for(int i = 1; i <= n ;i++)
            if(visx[i])
            for(int j = 1; j <= n ; j++)
                if(!visy[j])
                 a = min(a,Lx[i] + Ly[j] - map[i][j]);
        for(int i = 1; i <= n ;i++){
            if(visx[i]) Lx[i] -= a;
            if(visy[i]) Ly[i] += a;
        }
    }
    void KM()
    {
        memset(match,-1,sizeof(match));
        for(int i = 1 ; i <= n ; i++){
            Lx[i] = Ly[i] = 0;
            for(int j = 1; j <= n; j++){
                Lx[i] = max(Lx[i],map[i][j]);
            }
        }
        for(int i = 1; i <= n ;i++){
            for(; ;){
                memset(visx,0,sizeof(visx));
                memset(visy,0,sizeof(visy));
                if(find(i)) break;
                else update();
            }
        }
    }
    int main()
    {
        while(~scanf("%d",&n)){
        for(int i = 1;i <= n ;i++)
            for(int j = 1; j <= n ;j++)
            scanf("%d",&map[i][j]);
        KM();
        int res = 0;
        for(int i = 1; i <= n ; i++){
            if(match[i] != -1){
                res += map[match[i]][i];
            }
        }
        printf("%d
    ",res);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Atitit 图像处理30大经典算法attilax总结
    Atitit 图像清晰度 模糊度 检测 识别 评价算法 源码实现attilax总结
    Atitit  rgb yuv  hsv HSL 模式和 HSV(HSB) 图像色彩空间的区别
    Atitit  从 RGB 到 HSL 或 HSV 的转换
    Atitit 图像清晰度 模糊度 检测 识别 评价算法 原理
    Atitit 修改密码的功能流程设计 attilax总结
    atitit 点播系统 概览 v2 qb1.docx
    Atitit dsl exer v3 qb3 新特性
    atitit.TokenService v3 qb1  token服务模块的设计 新特性.docx
    Atitit 异常机制与异常处理的原理与概论
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4546434.html
Copyright © 2011-2022 走看看