首页 > 代码库 > HDU 3315 My Brute(KM最大匹配)

HDU 3315 My Brute(KM最大匹配)

HDU 3315 My Brute

题目链接

和HDU2835是一样的思路,利用把数字离散掉来多判断一个优先级

代码:

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

const int MAXNODE = 105;

typedef int Type;
const Type INF = 0x3f3f3f3f;

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

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

	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 < m; 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;
					right[i] = j;
					return true;
				}
			} else slack[j] = min(slack[j], tmp);
		}
		return false;
	}

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

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

const int N = 105;

int n, val[N], h1[N], h2[N], a1[N], a2[N];

int main() {
	while (~scanf("%d", &n) && n) {
		for (int i = 0; i < n; i++) scanf("%d", &val[i]);
		for (int i = 0; i < n; i++) scanf("%d", &h1[i]);
		for (int i = 0; i < n; i++) scanf("%d", &h2[i]);
		for (int i = 0; i < n; i++) scanf("%d", &a1[i]);
		for (int i = 0; i < n; i++) scanf("%d", &a2[i]);
		gao.init(n, n);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				int t1 = h1[i] / a2[j] + (h1[i] % a2[j] != 0);
				int t2 = h2[j] / a1[i] + (h2[j] % a1[i] != 0);
				if (t1 >= t2) gao.add_Edge(i, j, val[i] * (n + 1));
				else gao.add_Edge(i, j, -val[i] * (n + 1));
				if (i == j) gao.g[i][j]++;
			}
		}
		int ans = gao.km();
		if (ans <= 0) printf("Oh, I lose my dear seaco!\n");
		else {
			int cnt = 0;
			for (int i = 0; i < gao.n; i++)
				if (gao.right[i] == i) cnt++;
			printf("%d %.3lf%%\n", ans, cnt * 1.0 / n * 100);
		}
	}
	return 0;
}


HDU 3315 My Brute(KM最大匹配)