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 <algorithm>
using namespace std;

const int MAXNODE = 505;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct KM {
	int n;
	Type g[MAXNODE][MAXNODE];
	Type Lx[MAXNODE], Ly[MAXNODE], slack[MAXNODE];
	int left[MAXNODE];
	bool S[MAXNODE], T[MAXNODE];

	void init(int n) {
		this->n = n;
	}

	void add_Edge(int u, int v, Type val) {
		g[u][v] = val;
	}

	bool dfs(int i) {
		S[i] = true;
		for (int j = 0; j < n; j++) {
			if (T[j]) continue;
			Type tmp = Lx[i] + Ly[j] - g[i][j];
			if (!tmp) {
				T[j] = true;
				if (left[j] == -1 || dfs(left[j])) {
					left[j] = i;
					return true;
				}
			} else slack[j] = min(slack[j], tmp);
		}
		return false;
	}

	void update() {
		Type a = INF;
		for (int i = 0; i < n; i++)
			if (!T[i]) a = min(a, slack[i]);
		for (int i = 0; i < n; i++) {
			if (S[i]) Lx[i] -= a;
			if (T[i]) Ly[i] += a;
		}
	}

	void km() {
		for (int i = 0; i < n; i++) {
			left[i] = -1;
			Lx[i] = -INF; Ly[i] = 0;
			for (int j = 0; j < n; j++)
				Lx[i] = max(Lx[i], g[i][j]);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) slack[j] = INF;
			while (1) {
				for (int j = 0; j < n; j++) S[j] = T[j] = false;
				if (dfs(i)) break;
				else update();
			}
		}
	}
} gao;

int n;

int main() {
	while (~scanf("%d", &n)) {
		gao.init(n);
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++) {
				scanf("%d", &gao.g[i][j]);
			}
		gao.km();
		int ans = 0;
		for (int i = 0; i < n; i++) {
			printf("%d%c", gao.Lx[i], i == n - 1 ? '\n' : ' ');
			ans += gao.Lx[i];
		}
		for (int i = 0; i < n; i++) {
			printf("%d%c", gao.Ly[i], i == n - 1 ? '\n' : ' ');
			ans += gao.Ly[i];
		}
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2025-01-15 23:16:48

UVA 11383 - Golden Tiger Claw(二分图完美匹配扩展)的相关文章

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

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]仅需要取该行/列中最

UVA 1045 - The Great Wall Game(二分图完美匹配)

UVA 1045 - The Great Wall Game 题目链接 题意:给定一个n*n的棋盘,有n个棋子在上面,现在要移动棋子,每一步代价是1,现在要把棋子移动到一行,一列,或者在主副对角线上,问最小代价 思路:二分图完美匹配,枚举每种情况,建边,边权为曼哈顿距离,然后km算法做完美匹配算出值即可,由于要求最小值所以边权传负数,这样做出来的值的负就是答案 代码: #include <cstdio> #include <cstring> #include <cmath&g

UVA 10888 - Warehouse(二分图完美匹配)

UVA 10888 - Warehouse option=com_onlinejudge&Itemid=8&page=show_problem&category=562&problem=1829&mosmsg=Submission+received+with+ID+14222079" target="_blank" style="">题目链接 题意:就是推箱子游戏,问最少要几步 思路:每一个箱子和目标位置建边.

UVA 1411 - Ants(二分图完美匹配)

UVA 1411 - Ants 题目链接 题意:给定一些黑点白点,要求一个黑点连接一个白点,而且全部线段都不相交 思路:二分图完美匹配,权值存负的欧几里得距离,这种话,相交肯定比不相交权值小,所以做一次完美匹配就能够了 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXNODE = 1

POJ 1904 King&#39;s Quest强连通分量+二分图完美匹配

题目描述: Description Once upon a time there lived a king and he had N sons. And there were N beautiful girls in the kingdom and the king knew about each of his sons which of those girls he did like. The sons of the king were young and light-headed, so 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 少林决胜(二分图最佳完美匹配)

https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数col(i),使得对于任意格子(i,j),w(i,j)<=row(i)+col(j).所有的row(i)和col(i)只和应尽量小. 思路: 利用二分图最佳完美匹配当中的l(x)+l(y)>=w(i,j),直接用KM算法即可. 1 #include<iostream> 2 #inclu