码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive - 4043 Ants (KM裸题)

时间:2015-08-06 18:21:24      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:给出N只蚂蚁和N棵树的坐标,问如何完美匹配,才能使蚂蚁到树的连线不会相交

解题思路:KM裸题,但是很郁闷啊
不开根号,用long long竟然过不了,很无语啊,距离最大只有8亿啊
然后用A的double的代码,把他改成了long long ,WA了
然后再用double的A的代码,不开根了,又WA了,这题真坑到底是什么鬼

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 110
#define INF 1e20
#define esp 1e-10

struct Node{
    double x, y;
}Ants[N], Apple[N];

double w[N][N];
double lx[N], ly[N];
int left[N], n;
bool S[N], T[N];

void init() {
    for (int i = 1; i <= n; i++)
        scanf("%lf%lf", &Ants[i].x, &Ants[i].y);

    for (int i = 1; i <= n; i++)
        scanf("%lf%lf", &Apple[i].x, &Apple[i].y);

    double x1, x2, y1, y2;
    for (int i = 1; i <= n; i++) {
        x1 = Apple[i].x; 
        y1 = Apple[i].y;
        for (int j = 1; j <= n; j++) {
            x2 = Ants[j].x;
            y2 = Ants[j].y;
            w[i][j] = -sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        }
    }
}

bool match(int u) {
    S[u] = true;
    for (int j = 1; j <= n; j++) {
        if (fabs(lx[u] + ly[j] - w[u][j]) < esp && !T[j]) {
            T[j] = true;
            if (!left[j] || match(left[j])) {
                left[j] = u;
                return true;
            }
        }
    }
    return false;
}

void update() {
    double Min = INF;

    for (int i = 1; i <= n; i++) if (S[i]) 
        for (int j = 1; j <= n; j++) if (!T[j]) 
            Min = min(lx[i] + ly[j] - w[i][j], Min);

    for (int i = 1; i <= n; i++) {
        if (S[i]) lx[i] -= Min;
        if (T[i]) ly[i] += Min;
    }
}

void EK() {
    for (int i = 1; i <= n; i++) {

        left[i] = ly[i] = 0;
        lx[i] = -INF;
        for (int j = 1; j <= n; j++)
            lx[i] = max(lx[i], w[i][j]);
    }

    for (int i = 1; i <= n; i++) {
        while(1) {
            for (int j = 1; j <= n; j++)  S[j] = T[j] = 0;
            if (match(i)) break; else update();
        }
    }
}

int main() {
    int cnt = 0;
    while (scanf("%d", &n) != EOF) {
        if (cnt++)
            printf("\n");
        init();
        EK();
        for (int i = 1; i <= n; i++)
            printf("%d\n", left[i]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UVALive - 4043 Ants (KM裸题)

标签:

原文地址:http://blog.csdn.net/l123012013048/article/details/47320583

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!