uva 1411 Ants (权值和最小的完美匹配---KM算法)

uva 1411 Ants

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

Input has several dataset. The first line of each dataset 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(- 10000 x, y 10000) 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

For each dataset, 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 i -th ant colony. Print a blank line between datasets.

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个白点和黑点的坐标,要求用n条不相交的线段把它们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接到一条线段。
解题思路:点的坐标已经给出,那么点与点之间的距离就是它们的欧几里得距离( ((x1x2)2+(y1y2)2) 其实就是这个)。这题的难点在于,如何解决线段之间不能相交的问题。加入线段 (a,b) (c,d) 相交,那么一定有线段 (a,c) (b,d) 的权值和小于它,所以为了满足权值和最小的完美匹配,匹配时是不会出现线段交叉的问题的。
如何用权值和最大的完美匹配模板求权值和最小的完美匹配?有两种方法:
1)算距离时,把权值读为负数。
2)读入权值时,用一个很大的数,减去所有的权值,记录的是减出来的差。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 205;
const int INF = 50005;
typedef long long ll;
int n;
double X1[N], Y1[N];
double X2[N], Y2[N];
double W[N][N];
double Lx[N], Ly[N];
int left[N];
bool S[N], T[N];

bool match(int i) {
    S[i] = true;
    for (int j = 1; j <= n; j++) {
        if (fabs(Lx[i] + Ly[j] - W[i][j]) < 1e-9 && !T[j]) {
            T[j] = true;
            if (!left[j] || match(left[j])) {
                left[j] = i;
                return true;
            }
        }   
    }
    return false;
}

void update() {
    double a = INF; 
    for (int i = 1; i <= n; i++) {
        if (!S[i]) continue;
        for (int j = 1; j <= n; j++) {
            if (T[j]) continue; 
            a = min(a, Lx[i] + Ly[j] - W[i][j]);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (S[i]) Lx[i] -= a;
        if (T[i]) Ly[i] += a;
    }
}

void KM() {
    for (int i = 1; i <= n; i++) {
        left[i] = Lx[i] = Ly[i] = 0;    
        for (int j = 1; j <= n; j++) {
            Lx[i] = max(Lx[i], W[i][j]);    
        }
    }
    for (int i = 1; i <= n; i++) {
        while (1) {
            for (int j = 1; j <= n; j++) S[j] = T[j] = 0;
            if (match(i)) break;
            else update();
        }   
    }
}

double getDis(int x, int y) {
    return sqrt(pow(X1[x] - X2[y], 2) + pow(Y1[x] - Y2[y], 2));
} 

void input() {
    for (int i = 1; i <= n; i++) {
        scanf("%lf %lf", &X1[i], &Y1[i]);       
    }
    for (int i = 1; i <= n; i++) {
        scanf("%lf %lf", &X2[i], &Y2[i]);   
    }
}

void getW() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            W[j][i] = -getDis(i, j);    
        }   
    }
}

int main() {
    while (scanf("%d", &n) != EOF) {
        input();    
        getW();
        KM();
        for (int i = 1; i <= n; i++) {
            printf("%d\n", left[i]);    
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值