ACM学习历程—POJ3565 Ants(最佳匹配KM算法)

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

 

题目就是求类似图中实心到空心圆的连线,一一映射,使两两线段不相交。

有一种思路就是一开始让所有点对随意连接,然后让相交的线段进行调整,这里比较好理解,比如AC与BD相交,那么AD与BC就必然不相交了。这样的话需要的调整的次数似乎不是很好计算。

但是可以肯定的是,最终状态必然是两两不相交了。

可以发现上面相交的AC与BD,必然满足AD+BC < AC+BD。这里可以用两次三角形两边之和大于第三边进行证明。这一步让点对里面边的权值和变小了。

于是考虑,逆命题:是否当边AC与BD可以减小成AD与BC时,一定是相交的?

事实证明这个命题是不一定的,但是可以发现当可以减小成AD与BC时,AD和BC一定是不相交的。否则会导致AD+BC > AC+BD。

所以只要能减小边权的和,一定能保证不相交。那么最终状态就变成了边权和最小的状态,也就是最小匹配。可以采用KM算法进行。

似乎是数据问题,不能使用边的平方进行处理。要用double保存边的大小,然后等于0的判断,改成小于eps。

 

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long

using namespace std;

const int maxN = 105;
const double inf = 1e10;
int n;
struct Point
{
    double x, y;
}x[maxN], y[maxN];
int nx, ny;
int link[maxN];
double lx[maxN], ly[maxN], slack[maxN], w[maxN][maxN];//link表示和y相接的x值,lx,ly为顶标,nx,ny分别为x点集y点集的个数
bool visx[maxN], visy[maxN];

inline double dis(int i, int j)
{
    double ans = (x[i].x-y[j].x)*(x[i].x-y[j].x) + (x[i].y-y[j].y)*(x[i].y-y[j].y);
    return -sqrt(ans);
}

bool DFS(int x)
{
    visx[x] = true;
    for (int y = 1; y <= ny; y++)
    {
        if (visy[y])
            continue;
        double t = lx[x]+ly[y]-w[x][y];
        if (fabs(t) < 1e-5)
        {
            visy[y] = true;
            if (link[y] == -1 || DFS(link[y]))
            {
                link[y] = x;
                return true;
            }
        }
        else if (slack[y] > t)//不在相等子图中slack取最小的
            slack[y] = t;
    }
    return false;
}

void KM()
{
    memset(link, -1, sizeof(link));
    memset(ly, 0, sizeof(ly));
    for (int i = 1; i <= nx; i++)//lx初始化为与它关联边中最大的
        for (int j = 1; j <= ny; j++)
            if (j == 1 || w[i][j] > lx[i])
                lx[i] = w[i][j];

    for (int x = 1; x <= nx; x++)
    {
        for (int i = 1; i <= ny; i++)
            slack[i] = inf;
        for (;;)
        {
            memset(visx, false, sizeof(visx));
            memset(visy, false, sizeof(visy));
            if (DFS(x))//若成功(找到了增广轨),则该点增广完成,进入下一个点的增广
                break;//若失败(没有找到增广轨),则需要改变一些点的标号,使得图中可行边的数量增加。
                    //方法为:将所有在增广轨中(就是在增广过程中遍历到)的X方点的标号全部减去一个常数d,
                    //所有在增广轨中的Y方点的标号全部加上一个常数d
            double d = inf;
            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++)//修改顶标后,要把所有不在交错树中的Y顶点的slack值都减去d
                if (visy[i])
                    ly[i] += d;
                else
                    slack[i] -= d;
        }
    }

    for (int i = 1; i <= n; ++i)
        printf("%d\n", link[i]);
}

void input()
{
    nx = ny = n;
    for (int i = 1; i <= n; ++i)
        scanf("%lf%lf", &y[i].x, &y[i].y);
    for (int i = 1; i <= n; ++i)
        scanf("%lf%lf", &x[i].x, &x[i].y);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            w[i][j] = dis(i, j);
}

int main ()
{
    //freopen("test.in", "r", stdin);
    while (scanf ("%d", &n) != EOF)
    {
        input();
        KM();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/andyqsmart/p/4857274.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 为了打好ACM比赛,需要学习很多算法。以下是一些可能需要掌握的算法: 1. 基础算法:如搜索、贪心、分治和动态规划等。 2. 图论算法:如最短路径、最小生成树、网络流和匹配等。 3. 字符串算法:如KMP算法、Trie树和后缀数组等。 4. 数学算法:如高精度计算、组合数学、数论和矩阵计算等。 5. 计算几何算法:如凸包、最近点对和圆的交等。 以上只是一些常见的算法,还有很多其它算法也需要学习和掌握。当然,这只是学习算法的一部分,还需要掌握数据结构和算法的实现技巧。 ### 回答2: 打ACM竞赛需要学习的算法有很多,以下是其中一些常见的算法: 1. 基础算法:包括排序算法(如冒泡排序、插入排序、选择排序、快速排序、归并排序)、搜索算法(如二分搜索、深度优先搜索、广度优先搜索)、递归算法等。 2. 图算法:包括图的表示方法与存储结构、图的遍历算法(如深度优先搜索、广度优先搜索)、最短路径算法(如Dijkstra算法、Bellman-Ford算法、Floyd-Warshall算法)、最小生成树算法(如Prim算法、Kruskal算法)等。 3. 动态规划:包括最优子结构、状态转移方程、记忆化搜索等基本概念,以及常见的动态规划问题(如背包问题、最长公共子序列问题、最优二叉搜索树等)的解法。 4. 字符串算法:包括字符串匹配算法(如朴素字符串匹配、KMP算法、Boyer-Moore算法、Rabin-Karp算法)和字符串处理算法(如最长回文子串、最长公共子串)等。 5. 树与图算法:包括树的表示与遍历(如前序遍历、中序遍历、后序遍历、层次遍历)、树的构建与转换、图的表示与遍历(如深度优先搜索、广度优先搜索)、最短路径与最小生成树等。 6. 数学相关算法:包括排列组合、概率统计、数论、线性代数等数学基础知识,以及与之相关的算法(如中国剩余定理、欧几里得算法、素数判定算法等)。 以上仅为打ACM需要学习的部分重要算法,实际上还有很多其他算法值得学习和掌握。在学习算法的过程中,重要的是理解算法的原理和思想,进行练习和实践来提升解题能力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值