POJ 3565 Ants(二分图最小权匹配,KM算法)

题目链接: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 ≤ xy ≤ 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个点,n颗苹果树,给每个蚂蚁分配一颗苹果树,要求,总距离最小且任意两个蚂蚁和苹果树的连线不相交。

题目思路:

很容易看出是个二分图,就是处理不容易。首先应该想到的是总距离最小那么任意两个蚂蚁和苹果树的连线就一定不相交。然后把任意蚂蚁和苹果树的欧几里得距离作为二分图权值,因为求最小,所以权值取负值,然后套KM算法。

二分图的一些算法讲解(包括KM算法):二分图匹配模板(匈牙利算法、hopcroft-carp算法、KM算法)


代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 105;
const double DINF = 0xffffffffffff;

int nx,ny;
double g[N][N];
int linker[N];
double lx[N],ly[N];
double slack[N];
bool visx[N],visy[N];

struct node
{
    int x,y;
}ant[N],tree[N];

double dist(int i,int j)
{
    return sqrt(1.0*((ant[i].x-tree[j].x)*(ant[i].x-tree[j].x)+(ant[i].y-tree[j].y)*(ant[i].y-tree[j].y)));
}

bool DFS(int x)
{
    visx[x]=true;
    for(int y=1;y<=ny;y++)
    {
        if(visy[y]) continue;
        double tmp = lx[x]+ly[y]-g[x][y];
        if(fabs(tmp)<=EXP)
        {
            visy[y]=true;
            if(linker[y]==-1||DFS(linker[y]))
            {
                linker[y]=x;
                return true;
            }
        }
        else if(slack[y]>tmp)
            slack[y]=tmp;
    }
    return false;
}

void KM()
{
    MEM(linker,-1);
    MEM(ly,0);
    for(int i=1;i<=nx;i++)
    {
        lx[i]=-DINF;
        for(int j=1;j<=ny;j++)
            if(g[i][j]>lx[i])
                lx[i] = g[i][j];
    }

    for(int x=1;x<=nx;x++)
    {
        for(int i=1;i<=ny;i++)
            slack[i] = DINF;
        while(true)
        {
            MEM(visx,false);
            MEM(visy,false);
            if(DFS(x))
                break;
            double  d = DINF;
            for(int i=1;i<=ny;i++)
                if(!visy[i]&&d>slack[i])
                    d=slack[i];
            for(int i=1;i<=nx;i++)
                if(visx[i])
                    lx[i] -= d;
            for(int i=1;i<=ny;i++)
            {
                if(visy[i])
                    ly[i]+=d;
                else
                    slack[i]-=d;
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int n;

    while(~scanf("%d",&n))
    {
        nx = ny = n;
        for(int i=1;i<=n;i++)
            scanf("%d%d",&ant[i].x,&ant[i].y);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&tree[i].x,&tree[i].y);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                g[i][j] = -dist(i,j);
        KM();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(linker[j]==i)
                {
                    printf("%d\n",j);
                    break;
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值