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)