HDU 2426 interesting housing problem (km perfect match)

Source: Internet
Author: User
HDU 2426 interesting housing problem

Question Link

N students and M rooms. Given the preferences of some students who want to house, find an optimal solution to allocate a room for each student and maximize their liking, note that there is a pitfall in this question, that is, the students will not live in a room with negative liking.

Idea: it is obvious that the maximum matching problem of KM is that the degree of liking is negative and the edge is not connected directly.

Code:

#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], right[MAXNODE];bool S[MAXNODE], T[MAXNODE];void init(int n) {this->n = n;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)g[i][j] = -INF;}void add_Edge(int u, int v, Type val) {g[u][v] = max(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;right[i] = j;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;}}int km(int tot) {for (int i = 0; i < n; i++) {left[i] = -1; right[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();}}int ans = 0;for (int i = 0; i < tot; i++) {if (right[i] == -1) return -1;if (g[i][right[i]] == -1) return -1;ans += g[i][right[i]];}return ans;}} gao;int n, m, k;int main() {int cas = 0;while (~scanf("%d%d%d", &n, &m, &k)) {gao.init(max(n, m));int u, v, w;while (k--) {scanf("%d%d%d", &u, &v, &w);if (w >= 0) gao.add_Edge(u, v, w);}printf("Case %d: %d\n", ++cas, gao.km(n));}return 0;}


HDU 2426 interesting housing problem (km perfect match)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.