UVA 11383 Golden Tiger Claw (km algorithm)

Source: Internet
Author: User

UVA 11383 Golden Tiger Claw topic: Given a nxn matrix, each lattice has a positive integer w ( i Span style= "font-family:mathjax_main;" id= "mathjax-span-349" class= "Mo", j ) 。 Your task is to define an integer row (i) for each row, and each column also determines an integer c o l ( i ) , allowing for lattice (i,J) , W(I,J)<= RoW(I)+CoL(J) 。 All r o w ( i ) And c o l ( j ) The sum of the minimum. The idea of solving problems: km algorithm.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;Const intN = the;Const intINF =0x3f3f3f3f;typedef Long LongllintNintW[n][n];intLx[n], ly[n];intLeft[n];BOOLS[n], t[n];BOOLMatchinti) {S[i] =true; for(intj =1; J <= N; J + +) {if(Lx[i] + ly[j] = = W[i][j] &&! T[j]) {T[j] =true;if(!left[j] | | match (LEFT[J)) {Left[j] = i;return true; }        }       }return false;}voidUpdate () {intA = INF; for(inti =1; I <= N; i++) {if(! S[i])Continue; for(intj =1; J <= N; J + +) {if(T[j])Continue;        A = min (A, lx[i] + ly[j]-w[i][j]); }    } for(inti =1; I <= N; i++) {if(S[i]) Lx[i]-= A;if(T[i])    Ly[i] + = A; }}voidKM () {///initial time in order to make Lx[i]+ly[j]>=w[i, j] Constant set, so that lx[i] for all the edges associated with the vertex XI maximum weight, ly[j]=0     for(inti =1; I <= N; i++) {Left[i] = lx[i] = Ly[i] =0; for(intj =1; J <= N;            J + +) {Lx[i] = max (Lx[i], w[i][j]); }    } for(inti =1; I <= N; i++) { while(1) { for(intj =1; J <= N; J + +) S[j] = t[j] =0;if(Match (i)) Break;ElseUpdate (); }       }}voidInput () { for(inti =1; I <= N; i++) { for(intj =1; J <= N; J + +) {scanf("%d", &w[i][j]); }       }}intMain () { while(scanf("%d", &n)! = EOF) {input (); KM ();intsum = lx[1];printf("%d", lx[1]); for(inti =2; I <= N; i++) {printf("%d", Lx[i]);        Sum + = Lx[i]; }puts("");printf("%d", ly[1]); Sum + = ly[1]; for(inti =2; I <= N; i++) {printf("%d", Ly[i]);        Sum + = Ly[i]; }printf("\n%d\n", sum); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint.

UVA 11383 Golden Tiger Claw (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.