hdu2853assignment km algorithm

Source: Internet
Author: User

//n individual, Match m task, each person completes each task the efficiency is different//start with a matching scheme and now redesign the matching scheme to make the most efficient and save as much as possible//original matching scheme,//*1000 The ownership value, and then for the weight of the original matching edge +1, using km algorithm to find the maximum with the right to match ans//Then the final maximum efficiency is ans/1000, and the number of matching edges retained is Ans%mod#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intMAXN = -;Const intMoD = +;Const intINF =0x3f3f3f3f;intMATCH[MAXN], LX[MAXN], LY[MAXN], SLACK[MAXN];intW[MAXN][MAXN], VISX[MAXN], VISY[MAXN];intN, M;BOOLFindintx) {Visx[x] =1; for(inti =1; I <= m;i++) {if(Visy[i])Continue;intTMP = Lx[x] + ly[i]-w[x][i];if(TMP = =0) {Visy[i] =1;if(Match[i] = =-1|| Find (Match[i])) {match[i] = x;return true; }        }ElseSlack[i] = min (slack[i], TMP); }return false;}intKM () {memset(Match,-1,sizeof(match)) ;memset(Ly,0,sizeof(ly)) ; for(inti =1; I <= n;i++) {lx[i] =-inf; for(intj =1; J <= m;j++) lx[i] = max (Lx[i], w[i][j]); } for(inti =1; I <= n;i++) { for(intj =1; J <= m;j++) slack[j] = inf; while(1)        {memset(VISX,0,sizeof(VISX)) ;memset(Visy,0,sizeof(Visy)) ;if(Find (i)) Break;intd = inf; for(intj =1; J <= m;j++)if(!visy[j]) d = min (d, slack[j]); for(intj =1; J <= n;j++)if(Visx[j]) lx[j]-= D; for(intj =1; J <= m;j++)if(Visy[j]) ly[j] + = D;ElseSLACK[J]-= D; }    }intAns =0; for(inti =1; I <= m;i++)if(Match[i]! =-1) ans + = w[match[i]][i];returnAns;}intMain () {//freopen ("In.txt", "R", stdin);     while(~scanf("%d%d", &n, &m)) { for(inti =1; I <= n;i++) for(intj =1; J <= m;j++) {scanf("%d", &w[i][j]);         W[I][J] *= mod; }intsum =0; for(inti =1; I <= n;i++) {intAscanf("%d", &a);           sum + = W[i][a]/mod;       w[i][a]++; }intans = KM ();printf("%d%d\n", N-ans%mod, ans/mod-sum); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu2853assignment km algorithm

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.