Ants(最小权值二分图匹配)

传送门poj3565

描述

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself.
Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants travel from their colony to their feeding places and back using chemically tagged routes. The routes cannot intersect each other or ants will get confused and get to the wrong colony or tree, thus spurring a war between colonies.
Bill would like to connect each ant colony to a single apple tree so that all n routes are non-intersecting straight lines. In this problem such connection is always possible. Your task is to write a program that finds such connection.
Ants(最小权值二分图匹配)_第1张图片
On this picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles. One possible connection is denoted by lines.

输入

The first line of the input file contains a single integer number n (1 ≤ n ≤ 100) — the number of ant colonies and apple trees. It is followed by n lines describing n ant colonies, followed by n lines describing n apple trees. Each ant colony and apple tree is described by a pair of integer coordinates x and y (−10 000 ≤ x, y ≤ 10 000) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No three points are on the same line.

输出

Write to the output file n lines with one integer number on each line. The number written on i-th line denotes the number (from 1 to n) of the apple tree that is connected to the i-th ant colony.

样例

  • Input
    5
    -42 58
    44 86
    7 28
    99 34
    -13 -59
    -47 -44
    86 74
    68 -75
    -68 60
    99 -60
  • Output
    4
    2
    1
    5
    3

题解

  • 题意:n个蚂蚁n颗树,一个蚂蚁连一个树,连线不能相交,求出每个蚂蚁所连的树
  • Ants(最小权值二分图匹配)_第2张图片
    假设A、B为蚂蚁,C、D为苹果树。则存在两种匹配:第一种是AD、BC,第二种是AC、BD。
    根据三角形不等式AD+BC < AC+BD,最后得到很重要的一个性质——满足总路程之和最小
    的方案一定不相交。现在来构建二分图,一边为蚂蚁,另一边为苹果树,以距离为边权值,题
    目就变为了求带权二分图最小权和的最佳匹配。
  • 这里要用数组优化

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//数组优化
#include
#include
#include
#include
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
const double inf=0xffffffffffff;
const int maxn=1e2+7;
const int mod=1e9+7;
double coor[maxn<<1][2],line[maxn][maxn];
double wx[maxn],wy[maxn],visx[maxn],visy[maxn];
int matchx[maxn],matchy[maxn];
double minsub,a,b,t;
double tem[maxn];
int n;
int Find(int x){
visx[x]=1;
for(int i=1;i<=n;i++){
if(visy[i])continue;
double t=wx[x]+wy[i]-line[x][i];
if(fabs(t)<=1e-6){
visy[i]=1;
if(!matchy[i]||Find(matchy[i])){
matchx[x]=i;
matchy[i]=x;
return true;
}
}
else tem[i]=min(tem[i],t);
}
return false;
}
void Km(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
tem[j]=inf;
while(1){
minsub=inf;
INIT(visx,0);INIT(visy,0);
if(Find(i))break;
for(int j=1;j<=n;j++)
if(!visy[j]) minsub=min(tem[j],minsub);
for(int j=1;j<=n;j++){
if(visx[j]) wx[j]-=minsub;
if(visy[j]) wy[j]+=minsub;
else tem[j]-=minsub;
}
}
}
}
int main(){
while(~scanf("%d",&n)){
INIT(line,0);INIT(wy,0);
INIT(matchx,0);INIT(matchy,0);
for(int i=1;i<=2*n;i++)
scanf("%lf%lf",&coor[i][0],&coor[i][1]);
for(int i=1;i<=n;i++){
wx[i]=-inf;
for(int j=n+1;j<=2*n;j++){
a=coor[j][0]-coor[i][0];
b=coor[j][1]-coor[i][1];
t=sqrt(a*a+b*b);
line[i][j-n]=-t;
wx[i]=max(wx[i],-t);
}
}
Km();
for(int i=1;i<=n;i++)
printf("%d\n",matchx[i]);
}
return 0;
}

你可能感兴趣的:(Ants(最小权值二分图匹配))