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

UVA1411 Ants

时间:2019-10-06 16:54:40      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:print   class   als   script   sqrt   ==   画图   ace   pre   

\(\mathtt{UVA 1411}\) \(\mathtt{Ants}\)

\(\mathcal{Description}\)

给定一些黑点白点,要求一个黑点连接一个白点,并且所有线段都不相交。

\(\mathcal{Solution}\)

首先通过画图可以发现,要使所有线段都不相交可以使距离总和最小。所以题意就转化成为使距离总和最小的连接方式,自然联想到\(KM\)。我们可以先预处理出每一对白点和黑点之间的距离,因为要求的是最小连接方式,所以可以把距离取反来求最大值,限制条件为\(g[i] + b[i] = a[i][j]\)\(a[i][j]\)是取反后的距离,\(g[i]\)是黑点的期望值,\(b[i]\)是白点的期望值,复杂度为\(O(n^3)\)

\(\mathcal{Code}\)

#include<bits/stdc++.h>
using namespace std;
const int N = 110, INF = 1 << 30, Eps = 1e-9;
bool tfg[N], tfb[N];
int match[N], n,cnt ;
double a1[N], b1[N], a2[N], b2[N], a[N][N], b[N], g[N];
inline int read() {
    int x = 0, k = 1; char c = getchar();
    for (; c < 48 || c > 57; c = getchar()) k ^= (c == '-');
    for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48);
    return k ? x : -x;
}
bool pd(int x) {
    tfg[x] = true;
    for (int i = 1; i <= n; i++)
        if (fabs(g[x] + b[i] - a[x][i]) <  Eps && !tfb[i]) {
            tfb[i] = true;
            if (match[i] == -1) {
                match[i] = x;
                return true;
            }
            if (pd(match[i])) {
                match[i] = x;
                return true;
            }
        }
    return false;
}
inline void KM() {
    memset(match, -1, sizeof(match));
    memset(b, 0.0, sizeof(b));
    for (int i = 1; i <= n; i++) {
        g[i] = std::max(0.0, a[i][1]);
        for (int j = 2; j <= n; j++)
            g[i] = std::max(g[i], a[i][j]);
    }
    for (int i = 1; i <= n; i++) {
        while (true) {
            //printf("%d KM\n",i);
            memset(tfb, false, sizeof(tfb));
            memset(tfg, false, sizeof(tfg));    
            if (pd(i))
                break;
            double d = INF;
            for (int j = 1; j <= n; j++)
                if (tfg[j])
                    for (int k = 1; k <= n; k++)
                        if (!tfb[k])
                            d = std::min(d, g[j] + b[k] - a[j][k]);
            // if (cnt<=5) printf("%d %.2lf YES\n",i,d);
            for (int j = 1; j <= n; j++) {
                if (tfg[j])
                    g[j] -= d;
                if (tfb[j])
                    b[j] += d;
            }
            // if (i==1 && cnt<=5) for (int j=1; j<=n; j++) printf("%d %.2lf %.2lf\n",j,g[j],b[j]);
            // cnt++;
        }
    }
}
inline double dist(double x, double y, double xx, double yy) {
    return sqrt((x - xx) * (x - xx) + (y - yy) * (y - yy));
}
int main() {
    while (scanf("%d", &n) != EOF) {
        for (int i = 1; i <= n; i++)
            scanf("%lf%lf", &a1[i], &b1[i]);
        for (int i = 1; i <= n; i++) 
            scanf("%lf%lf", &a2[i], &b2[i]);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                a[j][i] = -dist(a1[i], b1[i], a2[j], b2[j]);
        KM();
        for (int i = 1; i <= n; i++)
            printf("%d\n", match[i]);
    }
    return 0;
}

UVA1411 Ants

标签:print   class   als   script   sqrt   ==   画图   ace   pre   

原文地址:https://www.cnblogs.com/wjnclln/p/11627394.html

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