POJ 3565 Ants 最小权匹配

题目:

Ants
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6852   Accepted: 2153   Special Judge

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

Source

 
题意:
白点,黑点各有n个,每对黑白点之间连一个线段,问在保证线段不相交的情况下,应该怎么连,使其距离和最小(距离为欧几里德距离)输出每个白点所对应的黑点编号。
解法:
当权值和最小值,一定不相交,所以做一个二分图的最小权匹配即可。
使用KM算法。
贴一个形象生动讲KM算法的博客:http://www.cnblogs.com/wenruo/p/5264235.html
 
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 110;
const double INF = 0xffffffffffff;
const double eps = 1e-6;

struct Node
{
    double x,y;
}Dot1[MAXN],Dot2[MAXN];

double Dist(Node a,Node b)
{
    return -sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int N,NX,NY;
double Map[MAXN][MAXN];
int link[MAXN];
double lx[MAXN],ly[MAXN],slack[MAXN];
int visx[MAXN],visy[MAXN];

int FindPath(int u)
{
    visx[u] = 1;
    for(int i = 1; i <= NY; ++i)
    {
        if(visy[i])
            continue;
        double temp = lx[u] + ly[i] - Map[u][i];
        if(fabs(temp) <= eps)
        {
            visy[i] = 1;
            if(link[i] == -1 || FindPath(link[i]))
            {
                link[i] = u;
                return 1;
            }
        }
        else
        {
            if(slack[i] > temp)
                slack[i] = temp;
        }
    }
    return 0;
}

void KM()
{
    memset(lx,0,sizeof(lx));
    memset(ly,0,sizeof(ly));
    memset(link,-1,sizeof(link));
    for(int i = 1; i <= NX; ++i)
        for(int j = 1; j <= NY; ++j)
            if(Map[i][j] > lx[i])
                lx[i] = Map[i][j];

    for(int i = 1; i <= NX; ++i)
    {
        for(int j = 1; j <= NY; ++j)
            slack[j] = INF;
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(FindPath(i))
                break;
            double d = INF;
            for(int j = 1; j <= NY; ++j)
                if(!visy[j] && d > slack[j])
                    d = slack[j];
            for(int j = 1; j <= NX; ++j)
                if(visx[j])
                    lx[j] -= d;
            for(int j = 1; j <= NY; ++j)
            {
                if(visy[j])
                    ly[j] += d;
                else
                    slack[j] -= d;
            }
        }
    }
}

struct point{
    double x,y;
};
point a[150];
point b[150];
double distcount(point a,point b)
{
    return -sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int ans[110];
int deal()
{
    while (scanf("%d", &N)!=EOF) {
        NX=NY=N;
        memset(Map,0,sizeof(Map));
        for (int i=1;i<=N;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
        for (int i=1;i<=N;i++) scanf("%lf%lf",&b[i].x,&b[i].y);
        for (int i = 1; i <= N; ++i)
            for (int j = 1; j <=N; ++j){
                Map[j][i]=distcount(a[i],b[j]);
            }
        KM();
       for (int i=1;i<=N;i++) printf("%d\n",link[i]);
    }
    return 0;
}
int main(){
    deal();
    return 0;
}

 

 
posted @ 2017-10-05 16:57  晓风微微  阅读(296)  评论(0编辑  收藏  举报