1047-best couple Good question ~

Source: Internet
Author: User

http://www.ifrog.cc/acm/problem/1047

The idea is simple, run a Floyd and then use the KM.

But here's the problem, and this could be n! = M. So what?

Can actually make up some non-existent points. To make n = M. Their weight is set to 0. It means that these people's collocation is not contributing to the answer. Note cannot be set to-inf. Because those points are also necessary to choose people, but they chose people, the equivalent of no choice (the value does not exist.) If they are set to-inf, they will change the answer.

There is a small trick is that, at the beginning, I was the map of the-1 of those points, and changed to INF, the INF is not connected, then the direct Floyd can be no more than the special sentence. Then the question comes again. If after running Floyd, still is not connected, then how to do? They have the right value but INF. When composing a new diagram, it is also necessary to set their weight to 0, which is the equivalent of no choice.

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 1e2 + -;intE[MAXN][MAXN];intte[2* maxn][2*MAXN];intMATCH[MAXN];//Match[col] = rowintVX[MAXN],VY[MAXN];intFX[MAXN],FY[MAXN];intN, M;intDFS (intu) {Vx[u]=1; inti;  for(i=1; i<=m; i++) {//Filter the value of N Navigator Col        if(vy[i]==0&&fx[u]+fy[i]==E[u][i]) {Vy[i]=1; if(match[i]==0||DFS (Match[i])) {Match[i]=u;//Match[col]=row;                return 1;//Match Success            }        }    }    return 0;//I can't find it, but behind it, it's going to run. Km}voiddo_km () {//    inti,j; intD=inf;  for(i=1; i<=n; i++) {//traverse the value of each driver row        if(vx[i]==1) {             for(j=1; j<=m; J + +) {//for him to traverse the value of the Navigator Col                if(!Vy[j]) {                    if(D> (fx[i]+fy[j]-E[i][j])) {D=fx[i]+fy[j]-E[i][j]; }                }            }        }    }     for(i=1; i<=n; i++) {        if(vx[i]==1) {Fx[i]-=D; Vx[i]=0;//Please 0        }        if(vy[i]==1) {//Fy[i] + =D; Vy[i]=0;//Love 0        }    }    return ;}intanskm () {memset (VX),0,sizeof(VX)); memset (VY,0,sizeof(VY)); memset (FX,0,sizeof(FX)); memset (FY,0,sizeof(FY)); memset (Match,0,sizeof(match)); //km, the first initialization of Fx,fy is part of the algorithm     for(intI=1; i<=n; i++) {//traverse the value of each driver rowfy[i]=0; Fx[i]=-inf;//infinitely Small         for(intj=1; j<=m; J + +) {//traverse the value of col for each navigator            if(Fx[i]<e[i][j]) {//Tacit Valuefx[i]=E[i][j]; }        }    }     for(intI=1; i<=n; i++) {//traverse the value of each driver rowmemset (VX,0,sizeof(VX)); memset (VY,0,sizeof(VY));  while(!dfs (i)) {//if he can't find a match, he realizes the KM algorithmDO_KM ();//when the KM is finished, DFS will still be on the node that you want to insert, because he doesn't have a match.        }//if (t = = m) break;    }    intans=0;  for(intI=1; i<=m; i++)//Traverse Navigator, col valueAns + = e[match[i]][i];//the input row is the driver, Col is the navigator//Match[i]: Navigator I and Driver Match[i] with Match[col]=row;    returnans;}voidWork () {memset (TE,0,sizeofTE); Memset (E,0,sizeofe); CIN>> N >>m;  for(inti =1; I <= n + m; ++i) { for(intj =1; J <= n + M; ++j) {cin>>Te[i][j]; if(Te[i][j] = =-1) Te[i][j] =inf; }    }     for(intK =1; K <= n + m; ++k) { for(inti =1; I <= n + m; ++i) { for(intj =1; J <= n + M; ++j) {if(Te[i][j] > Te[i][k] +Te[k][j]) {Te[i][j]= Te[i][k] +Te[k][j]; }            }        }    }//for (int i = 1; I <= n + m; ++i) {//For (int j = 1; J <= n + M; ++j) {//cout << te[i][j] << "";//        }//cout << Endl;//    }//cout << Endl;     for(inti =1; I <= N; ++i) { for(intj =1; J <= M; ++j) {E[i][j]= Te[i][n +J]; if(E[i][j] = =inf) {E[i][j]=0;//do not connect, to use 0 instead            }        }    }    if(N >m) { for(inti =1; I <= N; ++i) { for(intj = m +1; J <= N; ++j) {E[i][j]=0; }} M=N; } Else if(N <m) { for(inti = n +1; I <= m; ++i) { for(intj =1; J <= M; ++j) {E[i][j]=0; }} N=m; } cout<< anskm () <<Endl;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endifIOS; intT; CIN>>T;  while(t--) work (); return 0;}

1047-best couple Good question ~

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.