• 奔小康赚大钱 hdu 2255


    奔小康赚大钱

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4211    Accepted Submission(s): 1825


    Problem Description
    传说在遥远的地方有一个很富裕的村落,有一天,村长决定进行制度改革:又一次分配房子。
    这但是一件大事,关系到人民的住房问题啊。村里共同拥有n间房间,刚好有n家老百姓,考虑到每家都要有房住(假设有老百姓没房子住的话,easy引起不安定因素),每家必须分配到一间房子且仅仅能得到一间房子。
    还有一方面,村长和另外的村领导希望得到最大的效益,这样村里的机构才会有钱.因为老百姓都比較富裕,他们都能对每一间房子在他们的经济范围内出一定的价格,比方有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

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int maxn = 310;
    const int inf = 1<<30;
    int weight[maxn][maxn]; //记录全然二分图的全部边的权值
    int match[maxn];//记录全然二分图的完美匹配
    bool visx[maxn], visy[maxn];//记录x。y集合中的顶点是否出如今增广路中
    int lx[maxn], ly[maxn];//记录x,y各顶点的标记量
    int slack[maxn];//记录x在增广路,y不在增广路的每条边lx[x]+ly[y]-weight[x][y]到y的最小值放在slack[y]
                      //用来扩大相等子图(二分图的子图)。
    bool dfs(const int& x,const int& n)
    {
        visx[x] = 1; //x在增广路中出现
        for (int i = 0; i < n; i ++) {
            if (visy[i]) continue;
            int t = lx[x]+ly[i]-weight[x][i];
            if (t == 0) { //x在增广路,y在增广路
                visy[i] = 1;
                if (match[i] == -1 || dfs(match[i], n)) {
                    match[i] = x;
                    return true;
                }
            } else { //x在增广路。y不在增广路
                slack[i] = min(slack[i], t);
            }
        }
    return false;
    }
    
    int KM(const int& n)
    {
        memset(match, -1, sizeof(match)); //初始化,二分图的最大匹配为0
        for (int i = 0; i < n; i ++) {
                lx[i] = 0, ly[i] = 0;
            for (int j = 0; j < n; j ++) {
                lx[i] = max(lx[i], weight[i][j]);//初始化lx为x的邻接边最大权值。ly为0
            }
        }
    
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) slack[j] = inf;
            while (1) {
                memset(visx, 0, sizeof(visx)); //初始化x。y都不在增广路中
                memset(visy, 0, sizeof(visy));
                if (dfs(i, n)) break; //匹配成功,则继续求下个x点的最大匹配边。失败更新各点的标量。添加可行边的数量
    
                int d = inf;
                for (int j = 0; j < n; j ++) { //求起点在增广路,而终点不在增广路的边,端点标量和与边权差的最小值。

    if (!visy[j]) d = min(d, slack[j]); } for (int j = 0; j < n; j ++) { if (visx[j]) lx[j] -= d; } //更新增广路中x的标量 for (int j = 0; j < n; j ++) { if (visy[j]) ly[j] += d; //更新增广路中y的标量 else slack[j] -= d; //更新slack的最小值。

    (由于与Y中j相连的增广路x的标量都减小了d) } } } int res = 0; for (int i = 0; i < n; i ++) { //Y集合中可能有没有匹配的点 if (match[i] > -1)res += weight[match[i]][i]; } return res; } int main() { int n; while (scanf("%d", &n)!=EOF) { for (int i = 0; i < n; i ++) { for (int j = 0; j < n; j ++) { scanf("%d", &weight[i][j]); } } int ans = KM(n); printf("%d ", ans); } return 0; }



  • 相关阅读:
    十二道MR习题
    十二道MR习题 – 1 – 排序
    初识HBase
    Java内存分析1
    scala学习手记40
    scala学习手记40
    scala学习手记39
    scala学习手记38
    scala学习手记37
    scala学习手记36
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5172833.html
Copyright © 2020-2023  润新知