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

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   for   2014   

UVA 1045 - The Great Wall Game

題目連結

題意:給定一個n*n的棋盤,有n個棋子在上面,現在要移動棋子,每一步代價是1,現在要把棋子移動到一行,一列,或者在主副對角線上,問最小代價

思路:二分圖完美匹配,枚舉每種情況,建邊,邊權為曼哈頓距離,然後km演算法做完美匹配算出值即可,由於要求最小值所以邊權傳負數,這樣做出來的值的負就是答案

代碼:

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>using namespace std;const int MAXNODE = 105;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;}}Type 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();}}Type ans = 0;for (int i = 0; i < n; i++)ans += g[left[i]][i];return ans;}} gao;const int N = 20;int n, x[N], y[N];int dis(int x1, int y1, int x2, int y2) {return abs(x1 - x2) + abs(y1 - y2);}int main() {int cas = 0;while (~scanf("%d", &n) && n) {gao.init(n);for (int i = 0; i < n; i++) {scanf("%d%d", &x[i], &y[i]);x[i]--; y[i]--;}int ans = -1000;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {for (int k = 0; k < n; k++) {gao.add_Edge(j, k, -dis(x[j], y[j], i, k));}}ans = max(ans, gao.km());}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {for (int k = 0; k < n; k++) {gao.add_Edge(j, k, -dis(x[j], y[j], k, i));}}ans = max(ans, gao.km());}for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)gao.add_Edge(i, j, -dis(x[i], y[i], j, j));ans = max(ans, gao.km());for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)gao.add_Edge(i, j, -dis(x[i], y[i], n - j - 1, j));ans = max(ans, gao.km());printf("Board %d: %d moves required.\n\n", ++cas, -ans);}return 0;}


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.