HDU 3523 Image copy detection(KM最大匹配),hdu3523

來源:互聯網
上載者:User

HDU 3523 Image copy detection(KM最大匹配),hdu3523
HDU 3523 Image copy detection

題目連結

題意:這題其實題意讀懂就簡單了,說白了就是1-n放到1-n列,每列的值為每列上數字和該數位差的絕對值,然後求總和最小

思路:就一KM最大匹配

代碼:

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>using namespace std;const int MAXNODE = 505;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;memset(g, 0, sizeof(g));}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] == -1) return -1;//if (g[i][right[i]] == -INF) return -1;ans += g[i][right[i]];}return -ans;}} gao;int t, n, m;int main() {int cas = 0;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);gao.init(n, n);int tmp;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {scanf("%d", &tmp);for (int k = 0; k < n; k++)gao.add_Edge(k, j, -abs(tmp - k - 1));}}printf("Case #%d: %d\n", ++cas, gao.km());}return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.