UVALive 4043 Ants

KM   构图求最小权值匹配

保证最小的权值,所连的边一定是可以不相交的.

Ants

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]  
[Go Back]   [Status]

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(1n100) --
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(- 10000xy10000) 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

Source

Northeastern Europe 2007-2008

[Submit]  
[Go Back]   [Status]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn=220;
const double INF=999999999999999.;
const double eps=1e-5;

struct Dian
{
    double x,y;
}white[maxn*maxn],black[maxn*maxn];

int n;
double g[maxn][maxn];
int linker[maxn*maxn];
double lx[maxn*maxn],ly[maxn*maxn];
double slack[maxn*maxn];
bool visx[maxn*maxn],visy[maxn*maxn];

bool dfs(int x)
{
    visx[x]=true;
    for(int y=0;y<n;y++)
    {
        if(visy[y]) continue;
        double tmp=lx[x]+ly[y]-g[x][y];
        if(fabs(tmp)<eps)
        {
            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;
}

int KM()
{
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for(int i=0;i<n;i++)
    {
        lx[i]=-INF;
        for(int j=0;j<n;j++)
            if(g[i][j]>lx[i])
                lx[i]=g[i][j];
    }
    for(int x=0;x<n;x++)
    {
        for(int i=0;i<n;i++)
            slack[i]=INF;
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x)) break;
            double d=INF;
            for(int i=0;i<n;i++)
                if(!visy[i]&&d>slack[i])
                    d=slack[i];
            for(int i=0;i<n;i++)
                if(visx[i])
                    lx[i]-=d;
            for(int i=0;i<n;i++)
                if(visy[i])
                    ly[i]+=d;
                else
                    slack[i]-=d;
        }
    }
}

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

int main()
{
    bool first=false;
    while(scanf("%d",&n)!=EOF)
    {
        if(first) putchar(10);
        else first=true;
        for(int i=0;i<n;i++)
        {
            double a,b;
            scanf("%lf%lf",&a,&b);
            white[i]=(Dian){a,b};
        }
        for(int i=0;i<n;i++)
        {
            double a,b;
            scanf("%lf%lf",&a,&b);
            black[i]=(Dian){a,b};
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                double t=Dist(black[i],white[j]);
                g[i][j]=-t;
            }
        }
        KM();
        for(int i=0;i<n;i++)
        {
            printf("%d\n",linker[i]+1);
        }
    }
    return 0;
}

UVALive 4043 Ants,布布扣,bubuko.com

时间: 2024-08-27 00:58:38

UVALive 4043 Ants的相关文章

UVALive - 4043 Ants (KM裸题)

题目大意:给出N只蚂蚁和N棵树的坐标,问如何完美匹配,才能使蚂蚁到树的连线不会相交 解题思路:KM裸题,但是很郁闷啊 不开根号,用long long竟然过不了,很无语啊,距离最大只有8亿啊 然后用A的double的代码,把他改成了long long ,WA了 然后再用double的A的代码,不开根了,又WA了,这题真坑到底是什么鬼 #include <cstdio> #include <cstring> #include <algorithm> #include <

UVALive 4043 Ants 蚂蚁(二分图最佳完美匹配)

题意:n个蚂蚁n棵树,蚂蚁与树要配对,在配对成功的一对之间连一条线段,要求所有线段不能相交.按顺序输出蚂蚁所匹配的树. 思路:这个题目真是技巧啊,如果知道要求的就是整体最优,那么就容易做了.而不能用贪心来为每个蚂蚁选择最近的树,这样可能八成还是相交了. 整体最优能让每条线段不相交,证明: 假设a1-b1与a2-b2相交.则dis(a1,b1)+dis(a2,b2)>=dis(a1,b2)+dis(a2,b1).如果我们所决定的最优匹配是按照整体距离最短来匹配的,那么dis(a1,b1)+dis(

UVaLive 4043 Ants (最佳完美匹配)

题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交. 析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可以的,为什么呢,假设有两条线段a1-b1和a2-b2,那么如果相交,dist(a1, b1) + dist(a2, b2) > dist(a1, b2) + dist(a2, b1),自己画图看看就好,然后如果是最小的距离,那么就是可以的了,只要在用使得KM匹配时,把权值取反就好. 代码如下: #p

训练指南 UVALive - 4043(二分图匹配 + KM算法)

layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true mathjax: true tags: - 二分图匹配 - 图论 - 训练指南 Ants UVALive - 4043 题意 给你n个白点和n个黑点的平面坐标,要求用n条不相交的线连起来,每条线段连一个白点和黑点,每个点连一条线,也就是匹配.让你输出第i个白点所对应的黑点. 思路 二分图完美匹配问题.但是题目

UVALive 4043 转化最佳完美匹配

首先黑点和白点是组成一个二分图这毫无疑问 关键是题目中要求的所有黑白配的线不能交叉...一开始我也没想到这个怎么转化为二分图里面的算法. 后来看书才知道,如果两两交叉,则可以把两根线当四边形的对角线,连四边形的两条边,则肯定不交叉,而且一个很明显的特征是,不交叉的两条线的他们的长度和 一定比交叉线的长度和小. 于是我们只要求出最小长度的线,就必定是不相交的.那就要用到最佳完美匹配了,首先算出两两点的欧几里得距离,然后取负数,这样走一遍匹配,得到的必定是最短的欧几里得距离,即不相交的线 #incl

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu