uva 11383 Golden Tiger Claw (KM算法)

uva 11383 Golden Tiger Claw

题目大意:给定一个N×N的矩阵,每个格子里都有一个正整数w(i,j)。你的任务是给每行确定一个整数row(i), 每列也确定一个整数col(i),使得对于格子(i,j),w(i,j)<=row(i)+col(j)。所有row(i)和col(j)的总和最小。

解题思路:KM算法。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 5000;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n;
int W[N][N];
int Lx[N], Ly[N];
int left[N];
bool S[N], T[N];

bool match(int i) {
    S[i] = true;
    for (int j = 1; j <= n; j++) {
        if (Lx[i] + Ly[j] == W[i][j] && !T[j]) {
            T[j] = true;
            if (!left[j] || match(left[j])) {
                left[j] = i;
                return true;
            }
        }
    }
    return false;
}

void update() {
    int a = INF;
    for (int i = 1; i <= n; i++) {
        if (!S[i]) continue;
        for (int j = 1; j <= n; j++) {
            if (T[j]) continue;
            a = min(a, Lx[i] + Ly[j] - W[i][j]);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (S[i]) Lx[i] -= a;
        if (T[i]) Ly[i] += a;
    }
}

void KM() {
    //初始时为了使Lx[i]+Ly[j]>=W[i, j]恒成立,令Lx[i]为所有与顶点Xi关联的边的最大权,Ly[j]=0
    for (int i = 1; i <= n; i++) {
        left[i] = Lx[i] = Ly[i] = 0;
        for (int j = 1; j <= n; j++) {
            Lx[i] = max(Lx[i], W[i][j]);
        }
    }
    for (int i = 1; i <= n; i++) {
        while (1) {
            for (int j = 1; j <= n; j++) S[j] = T[j] = 0;
            if (match(i)) break;
            else update();
        }
    }
}

void input() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%d", &W[i][j]);
        }
    }
}

int main() {
    while (scanf("%d", &n) != EOF) {
        input();
        KM();
        int sum = Lx[1];
        printf("%d", Lx[1]);
        for (int i = 2; i <= n; i++) {
            printf(" %d ", Lx[i]);
            sum += Lx[i];
        }
        puts("");
        printf("%d", Ly[1]);
        sum += Ly[1];
        for (int i = 2; i <= n; i++) {
            printf(" %d", Ly[i]);
            sum += Ly[i];
        }
        printf("\n%d\n", sum);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不可转载。

时间: 2024-08-07 17:01:19

uva 11383 Golden Tiger Claw (KM算法)的相关文章

UVA 11383 - Golden Tiger Claw(二分图完美匹配扩展)

UVA 11383 - Golden Tiger Claw 题目链接 题意:给定每列和每行的和,给定一个矩阵,要求每个格子(x, y)的值小于row(i) + col(j),求一种方案,并且所有行列之和的和最小 思路:A二分图完美匹配的扩展,行列建二分图,权值为矩阵相应位置的值,做一次KM算法后,所有顶标之和就是最小的 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algor

UVA 11383 Golden Tiger Claw

Golden Tiger Claw Time Limit: 8000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1138364-bit integer IO format: %lld      Java class name: Main Omi, Raymondo, Clay and Kimiko are on new adventure- in search of new Shen Gon

UVA 11383 Golden Tiger Claw 金虎爪(KM算法)

题意:给一个n*n的矩阵,每个格子中有正整数w[i][j],试为每行和每列分别确定一个数字row[i]和col[i],使得任意格子w[i][j]<=row[i]+col[j]恒成立.先输row,再输出col,再输出全部总和(总和应尽量小). 思路: KM算法中的顶标就是保持了Lx[i]+ly[j]>=g[i][j]再求最大权和匹配的,但这个最大权和并没有关系.我们可以将row[i]看成一个男的,col[i]看成一个女的,这样男女的总数就相等.一般来说,Lx[i]或Ly[i]仅需要取该行/列中最

UVA11383 Golden Tiger Claw

题目 UVA11383 Golden Tiger Claw 做法 \(KM\)好题啊,满足所有边\(l(x)+l(y)≥w(x,y)\)(个人理解,如不对请及时留言),这样能满足\(\sum\limits_i^n(l(x)+l(y))\)最小值 My complete code #include<bits/stdc++.h> using namespace std; typedef long long LL; const LL maxn=1e3,inf=0x3f3f3f3f; LL n,mi;

训练指南 UVA - 11383(KM算法的应用 lx+ly &gt;=w(x,y))

layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" catalog: true mathjax: true tags: - KM算法 - 训练指南 Golden Tiger Claw UVA - 11383 题意 给一个n*n的矩阵,每个格子中有正整数w[i[j],试为每行和每列分别确定一个数字row[i]和col[i],使得任意格子w[i][j]<=row[i

UVA 11383 KM性质

点击打开链接 题意:一个n*n的矩阵每个格子里有一个正整数w(i,j)你的任务是确定每行一个整数row(i)每列一个整数col(i),对每个格子都有w(i,j)<=row(i)+col(j)所有row(i)和col(i)和尽量小. 思路:本题利用KM算法l(x)+l(y)>=w(x,y)的性质直接可以知道得出的顶标之和即为最小的. #include <stdio.h> #include <string.h> #include <iostream> #incl

hdoj 3488 Tour 【最小费用最大流】【KM算法】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2299    Accepted Submission(s): 1151 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

hdu2255 奔小康赚大钱 二分图最佳匹配--KM算法

传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住的话,容易引起不安定因素),每家必须分配到一间房子且只能得到一间房子.另一方面,村长和另外的村领导希望得到最大的效益,这样村里的机构才会有钱.由于老百姓都比较富裕,他们都能对每一间房子在他们的经济范围内出一定的价格,比如有3间房子,一家老百姓可以对第一间出10万,对第2间出2万,对第3间出20万.(

Going Home(最大匹配km算法)

Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20115   Accepted: 10189 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertica