LA 4043 - Ants 求完美匹配

点击打开链接

 

 

4043 - Ants

Time limit: 3.000 seconds

 

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.

\epsfbox{p4043.eps}

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$ \le$n$ \le$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$ \le$x, y$ \le$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条不相交的线段将它们连接在一起,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接到一条线段。求第i个白点所连接的黑点编号。

构造一个二分图,每个白点对应一个X节点,每个黑点对应一个Y结点,每个黑点和白点相连,权值等于它们的距离。最佳完美匹配就是它的解。

 

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
int n;
double g[117][117],lx[117],ly[117];
int match[117];
bool visx[117],visy[117];
double wx[117],wy[117],bx[117],by[117];

bool dfs(int cur)
{
    visx[cur]=true;
    for(int y=1; y<=n; y++)
    {
        int t=lx[cur]+ly[y]-g[cur][y];
        if(t==0&&!visy[y])
        {
            visy[y]=true;
            if(!match[y]||dfs(match[y]))
            {
                match[y]=cur;
                return true;
            }
        }
    }
    return false;
}

void update()
{
    double d=inf;
    for(int i=1; i<=n; i++)
    {
        if(visx[i])
            for(int j=1; j<=n; j++)
                if(!visy[j])
                    d=min(d,lx[i]+ly[j]-g[i][j]);
    }
    for(int i=1; i<=n; i++)
    {
        if(visx[i])lx[i]-=d;
        if(visy[i])ly[i]+=d;
    }
}
void KM()
{
    for(int i=1; i<=n; i++)
    {
        lx[i]=ly[i]=match[i]=0;
        for(int j=1; j<=n; j++)
        {
            lx[i]=max(lx[i],g[i][j]);
        }
    }
    for(int x=1; x<=n; x++)
    {
        while(true)
        {
            for(int j=1;j<=n;j++)visx[j]=visy[j]=false;
            if(dfs(x))break;
            else update();
        }
    }
}
int main()
{
    bool first=false;
    while(scanf("%d",&n)!=EOF)
    {
        if(first)printf("\n");
        first=true;
        for(int i=1; i<=n; i++)
        {
            scanf("%lf%lf",&wx[i],&wy[i]);
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%lf%lf",&bx[i],&by[i]);
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                g[j][i]=-sqrt((wx[i]-bx[j])*(wx[i]-bx[j])+(wy[i]-by[j])*(wy[i]-by[j]));
            }
        KM();
        for(int i=1; i<=n; i++)
            printf("%d\n",match[i]);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ubuntu 18.04系统上安装ANTS(Advanced Normalization Tools),可以按照以下步骤进行: 1. 首先,确保您的系统已经安装了必要的依赖项。您可以使用包管理器apt来安装这些依赖项。打开终端并输入以下命令: ``` sudo apt update sudo apt install build-essential cmake git sudo apt install zlib1g-dev libcurl4-openssl-dev libinsighttoolkit4-dev libpng-dev libgdcm-tools ``` 2. 接下来,您需要克隆ANTS的GitHub存储库。在终端中,使用以下命令将存储库克隆到您的本地计算机上: ``` git clone https://github.com/ANTsX/ANTs.git ``` 3. 进入克隆的ANTS存储库目录: ``` cd ANTs ``` 4. 现在,您可以开始编译和安装ANTS。运行以下命令来配置和构建ANTS: ``` mkdir build cd build cmake .. make ``` 5. 完成编译后,您可以使用以下命令安装ANTS: ``` sudo make install ``` 6. 安装完成后,您可以验证ANTS是否成功安装。在终端中,输入以下命令以检查ANTS版本: ``` antsRegistrationSyNQuick -h ``` 如果成功安装,您将看到ANTS的帮助信息。 以上是在Ubuntu 18.04系统上安装ANTS的步骤。如果需要更详细的安装说明,您可以参考ANTS的GitHub安装说明链接:。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Ubuntu 18.04 下如何安装ANTs](https://blog.csdn.net/weixin_43156127/article/details/119718395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Ubuntu18.04安装opencv 3.2.0的解决方法](https://download.csdn.net/download/weixin_38637983/12842575)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值