zoukankan      html  css  js  c++  java
  • [题解] poj 3565 Ants (二分图最佳完美匹配)

    - 传送门 -

     http://poj.org/problem?id=3565

    #Ants | **Time Limit:** 5000MS |   | **Memory Limit:** 65536K | | **Total Submissions:** 6758 |   | **Accepted:** 2131 |   | Special Judge |

    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

    [[Submit](http://poj.org/submit?problem_id=3565)]   [[Status](http://poj.org/problemstatus?problem_id=3565)]   [[Discuss](http://poj.org/bbs?problem_id=3565)]
      ### - 题意 -  给出 n 个白点, n 个黑点的坐标, 要用 n 条不相连的线段将它们连起来, 每条线段连一个白点和一个黑点.   ### - 思路 -  终点在于我们要使答案最小, 因为如果对于白点(A1, A2)和黑点(B1, B2), A1-B1 与 A2-B2 相交的话, A1-B2 加 A2-B1 一定会更小, (可以画图证明.) 所以最小解一定是一个答案.  理解之后就是裸的二分图匹配了.  二分图最佳完美匹配求最大答案, 所以我们要对边权取反来操作.    细节见代码.   ### - 代码 - ```c++ #include #include #include #include using namespace std;

    typedef double db;
    const int N = 105;
    const double inf = 1000000000;
    const double eps = 1e-6;

    db MAP[N][N], cx[N], cy[N];
    int line[N], usex[N], usey[N];
    int X[N], Y[N];
    int n;
    int x, y;

    db mk(int x1, int x2, int y1, int y2) {
    return -1.0 * sqrt((x1 - x2)(x1 - x2) + (y1 - y2)(y1 - y2));
    }

    bool find(int x) {
    usex[x] = 1;
    for (int i = 1; i <= n; ++i) {
    if (usey[i] == 0 && fabs(cx[x] + cy[i] - MAP[x][i]) <= eps) {
    usey[i] = 1;
    if (line[i] == 0 || find(line[i])) {
    line[i] = x;
    return true;
    }
    }
    }
    return false;
    }

    void km() {
    for (int i = 1; i <= n; ++i) {
    while (true) {
    db d = inf;
    memset(usex, 0, sizeof (usex));
    memset(usey, 0, sizeof (usey));
    if (find(i)) break;
    for (int j = 1; j <= n; ++j) {
    if (usex[j])
    for (int k = 1; k <= n; ++k)
    if (!usey[k] && d > cx[j] + cy[k] - MAP[j][k])
    d = cx[j] + cy[k] - MAP[j][k];
    }
    if (d == inf) return;
    for (int j = 1; j <= n; ++j)
    if (usex[j]) cx[j] -= d;
    for (int j = 1; j <= n; ++j)
    if (usey[j]) cy[j] += d;
    }
    }
    for (int i = 1; i <= n; ++i) {
    printf("%d ", line[i]);
    }
    }

    int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &X[i], &Y[i]);
    }
    for (int i = 1, x, y; i <= n; ++i) {
    scanf("%d%d", &x, &y);
    db d = 0;
    for (int j = 1; j <= n; ++j) {
    MAP[i][j] = mk(X[j], x, Y[j], y);
    if (MAP[i][j] > d) d = MAP[i][j];
    }
    cx[i] = d;
    }
    km();
    return 0;
    }

    
    
      [1]: http://www.cnblogs.com/Anding-16/p/7410725.html
  • 相关阅读:
    uboot学习之三-----uboot启动第一阶段--start.S之一
    4.7 C语言的存储类,作用域,生命周期,链接属性
    uboot学习之二----主Makefile学习之一----版本号 u_boot_version(U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL).$(EXTRAVERSION)) (24-29行)
    uboot学习之二----主Makefile学习之二----环境变量之:主机的操作系统和主机架构(HOSTOS、HOSTARCH)(31-43行)
    uboot学习之二----主Makefile学习之三----静默编译
    uboot学习之二----主Makefile学习之四----两种编译方法:原地编译和单独输出文件夹编译
    Linux下安装交叉工具链&&安装vim
    善变的不只是女人,还有volatile ---偷来的标题名
    东芝半导体最新ARM开发板——TT_M3HQ开箱评测
    STM32F407外部晶体改为25M后检测不到芯片的解决办法
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7470014.html
Copyright © 2011-2022 走看看