uva1411 最小值转最大值+二分图匹配

时间:2024-10-13 23:36:20

这题给了n个白点和n个黑点坐标,计算出他们两两配对的总路程最少,

我们算出他们之间的距离,为d,然后 w[j][i]=-d; 就将求最小值转化为求最大值,然后采用km进行匹配计算

 #include <algorithm>
#include <string.h>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
/* KM算法
* 复杂度O(nx*nx*ny)
* 求最大权匹配
* 若求最小权匹配,可将权值取相反数,结果取相反数
* 点的编号从0开始
*/
const int N = ;
const double INF = 1000000.0;
int nx,ny; //两边的点数
double W[N][N]; //二分图描述
int Left[N];//y中各点匹配状态
double Lx[N],Ly[N]; // x,y中的点标号
double slack[N];
bool S[N],T[N];
int fab(double a, double b){
if(fabs(a-b)<0.00000000001) return ;
else return a-b>?:-;
}
bool DFS(int x) {
S[x] = true;
for(int y = ; y < ny; y++){
if(T[y]) continue;
double tmp = Lx[x] + Ly[y] - W[x][y];
if(fab(tmp,)==){
T[y] = true;
if(Left[y] == - || DFS(Left[y])){
Left[y] = x;
return true;
}
}
else if(slack[y] > tmp)
slack[y] = tmp;
}
return false;
}
void KM(){
memset(Left, -, sizeof(Left));
memset(Ly,, sizeof(Ly));
for(int i = ;i < nx;i++){
Lx[i] = -INF;
for(int j = ;j < ny;j++)
if(W[i][j] > Lx[i])
Lx[i] = W[i][j];
}
for(int x = ;x < nx;x++){
for(int i = ;i < ny;i++)
slack[i] = INF;
while(true){
memset(S, false, sizeof(S));
memset(T, false, sizeof(T));
if(DFS(x)) break;
double d = INF;
for(int i = ;i < ny;i++)
if(!T[i] && d > slack[i])
d = slack[i];
for(int i = ;i < nx;i++)
if(S[i])
Lx[i] -= d;
for(int i = ;i < ny;i++){
if(T[i])Ly[i] += d;
else slack[i] -= d;
}
}
}
}
//HDU 2255
double x1[N],x2[N],yy1[N],yy2[N];
int main()
{
int n,cccc=;
while(scanf("%d",&n) == ){
if(cccc++)printf("\n");
for(int i = ;i < n;i++)
scanf("%lf%lf",&x1[i],&yy1[i]);
for(int i=; i<n; i++)
scanf("%lf%lf",&x2[i],&yy2[i]);
for(int i=; i<n; i++)
for(int j=; j<n; j++)
W[j][i]=-sqrt((x1[i]-x2[j])*(x1[i]-x2[j])+
(yy1[i]-yy2[j])*(yy1[i]-yy2[j])
);
nx = ny = n;
KM();
for(int i=; i<n ;i++)
printf("%d\n",Left[i]+);
}
return ;
}