题目链接:http://poj.org/problem?id=3565
Description
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.
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.
Input
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.
Output
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.
Sample Input
5
-42 58
44 86
7 28
99 34
-13 -59
-47 -44
86 74
68 -75
-68 60
99 -60
Sample Output
4
2
1
5
3
题意:
白点,黑点各有n个,每对黑白点之间连一个线段,问在保证线段不相交的情况下,应该怎么连,使其距离和最小(距离为欧几里德距离)输出每个白点所对应的黑点编号。
思路:很明显是二分图最小权值匹配问题,由于KM算法求的是最大权值匹配,故要将权值取负,然后用 (KM) 模板即可,最后结果再取反输出
对于KM算法,我还是处于懵懂状态,需要继续理解。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 105;
int a[N], b[N], c[N], d[N];
double w[N][N]; // 边权
double la[N], lb[N]; // 左、右部点的顶标
bool va[N], vb[N]; // 访问标记:是否在交错树中
int match[N], ans[N]; // 右部点匹配了哪一个左部点
int n;
double upd[N], delta;
bool dfs(int x) {
va[x] = 1; // 访问标记:x在交错树中
for (int y = 1; y <= n; y++)
if (!vb[y])
if (fabs(la[x] + lb[y] - w[x][y]) < 1e-8) { // 相等子图
vb[y] = 1; // 访问标记:y在交错树中
if (!match[y] || dfs(match[y])) {
match[y] = x;
return true;
}
}
else upd[y] = min(upd[y], la[x] + lb[y] - w[x][y]);
return false;
}
void KM() {
for (int i = 1; i <= n; i++) {
la[i] = -1e10; // -inf
lb[i] = 0;
for (int j = 1; j <= n; j++)
la[i] = max(la[i], w[i][j]);
}
for (int i = 1; i <= n; i++)
while (true) { // 直到左部点找到匹配
memset(va, 0, sizeof(va));
memset(vb, 0, sizeof(vb));
delta = 1e10; // inf
for (int j = 1; j <= n; j++) upd[j] = 1e10;
if (dfs(i)) break;
for (int j = 1; j <= n; j++)
if (!vb[j]) delta = min(delta, upd[j]);
for (int j = 1; j <= n; j++) { // 修改顶标
if (va[j]) la[j] -= delta;
if (vb[j]) lb[j] += delta;
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]);
for (int i = 1; i <= n; i++) scanf("%d%d", &c[i], &d[i]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
w[i][j] = -sqrt((a[i]-c[j])*(a[i]-c[j])*1.0+(b[i]-d[j])*(b[i]-d[j])*1.0);
KM();
for (int i = 1; i <= n; i++) ans[match[i]] = i;
for (int i = 1; i <= n; i++) printf("%d
", ans[i]);
return 0;
}
贴一个形象生动讲KM算法的博客:http://www.cnblogs.com/wenruo/p/5264235.html