【POJ】3565Ants(二分图最优匹配)

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.

题目大意:现在有N个蚂蚁,N个苹果,然后告诉你这些蚂蚁的找到苹果的路径不能相交,让我们找出蚂蚁选择的苹果

思路:这个其实很容易就可以看出来是二分图的题目了,因为蚂蚁群体内部和苹果内部是没有任何关联的,有关联的就是两个群体之间的路径,那么我们怎样来处理这些路径呢?

其实很溶体就可以看出来,如果两个蚂蚁的路径是相交的,那么只要把他们想找到的苹果交换那么他们就不会相交了,

可以画个图试试看,并且在交换后每一个蚂蚁的路径都比之前的要短,那么这就是我们这个题的解题点了,找到的就是最小权值的匹配,根据他们的距离,然后跑一边二分图最优匹配就可以了

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#define eps 1e-4
using namespace std;
const int maxn=100+10;
const double inf = 1e17;
double w[maxn][maxn];
double la[maxn],lb[maxn];
bool va[maxn],vb[maxn];
int match[maxn];
double slack[maxn];
int n;
struct Node
{
    double x,y;
} a[maxn],b[maxn];
/*
5
-42 58
44 86
7 28
99 34
-13 -59
-47 -44
86 74
68 -75
-68 60
99 -60
*/
bool DFS(int u)
{
    va[u]=1;
    for(int v=1; v<=n; v++)
    {
        if(vb[v])
            continue;
        double t = la[u] + lb[v] -w[u][v];
        if(fabs(t) < eps)
        {
            vb[v]=1;
            if(!match[v]||DFS(match[v]))
            {
                match[v]=u;
                return true;
            }
        }
        else
            slack[v]=min(slack[v], t);
    }
    return false;
}
void KM()
{
    memset(match, 0, sizeof(match));
    for(int i = 1; i <=n; i++)
        lb[i] = 0;
    for(int i = 1; i <=n; i++)
    {
        la[i] = -inf;
        for(int j=1; j<=n; j++)
        {
            la[i] = max(la[i], w[i][j]);
        }
    }
    for(int x = 1; x <=n; x++)
    {
        for(int i = 1; i <=n; i++)
            slack[i] = inf;
        while(true)
        {
            memset(va, false, sizeof(va));
            memset(vb, false, sizeof(vb));
            if(DFS(x))
                break;
            double min_slack = inf;
            for(int i = 1; i <= n; i++)
                if(va[i])
                    for(int j = 1; j <= n; j++)
                        if(!vb[j])
                            min_slack = min(min_slack, la[i]+lb[j] - w[i][j]);
            //cout<<"min_slack: "<<min_slack<<endl;
            for(int i = 1; i <= n; i++)
            {
                if(va[i])
                    la[i] -= min_slack;
            }
            for(int i = 1; i <=n; i++)
            {
                if(vb[i])
                    lb[i] += min_slack;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        printf("%d\n",match[i]);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%lf%lf",&b[i].x,&b[i].y);
    for(int i=1; i<=n; i++)
        scanf("%lf%lf",&a[i].x,&a[i].y);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            w[i][j]=-sqrt((a[i].x-b[j].x)*(a[i].x-b[j].x)+(a[i].y-b[j].y)*(a[i].y-b[j].y));
    /*for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cout<<w[i][j]<<" ";
        cout<<endl;
    }*/
    KM();
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值