poj 3565 km算法

Ants
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3709   Accepted: 1125   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 ≤ x, y ≤ 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

Source

 
参考了白书,这是第二次做,复杂度O(n^3),简单说下算法的转化:见下图,
设所求最小匹配中A1,B1线段与A2,B2线段相交于D点。 由于A1D+DB2>A1B2,A2D+DB1>A2B1,累加,所以
A1B1+A2B2>A1B2+A2B1, 用不等式右边两条线段代替最小匹配中不等式左边线段会使最小匹配值更小,与”最小匹配“定义矛盾,故假设错误,因此可以用求最小匹配的方法求解该问题:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<cfloat>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 101
#define INF 100000000

int ytox[MAXN], n;
double lx[MAXN], ly[MAXN], slack[MAXN];
double w[MAXN][MAXN];
bool s[MAXN], t[MAXN];

struct point{
    int x, y;
}ant[MAXN], tree[MAXN];

#define zero(d) ((d) < 1e-9 ? 1 : 0)

bool dfs(int x){
    s[x]=true;
    for(int y=1; y<=n; y++){
        if(t[y]) continue;
        double d=lx[x]+ly[y]-w[x][y];
        if(zero(d)){
            t[y]=true;
            if(!ytox[y] || dfs(ytox[y])){
                ytox[y]=x;
                return true;
            }
        }
        else slack[y]=MIN(slack[y], d);
    }
    return false;
}

void km(){
    int i,j;
    memset(ly, 0, sizeof(ly));
    memset(ytox, 0, sizeof(ytox));
    for(i=1; i<=n; i++)
        for(j=1, lx[i]=-INF; j<=n; j++)
            lx[i]=MAX(lx[i], w[i][j]);

    for(i=1; i<=n; i++){
        for(j=1; j<=n; j++) slack[j]=INF;

        while(1){
            memset(s, 0, sizeof(s));
            memset(t, 0, sizeof(t));
            if(dfs(i)) break;

            double d=INF;
            for(j=1; j<=n; j++) if(!t[j])
                d=MIN(d, slack[j]);
            for(j=1; j<=n; j++) if(s[j])
                lx[j]-=d;
            for(j=1; j<=n; j++){
                if(t[j]) ly[j]+=d;
    //            else slack[j]-=d;
            }
        }
    }
}

int main(){
    //freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    int m=0;
    while(scanf(" %d", &n)==1){
        int i,j;
        for(i=1; i<=n; i++) scanf(" %d %d", &ant[i].x, &ant[i].y);
        for(i=1; i<=n; i++) scanf(" %d %d", &tree[i].x, &tree[i].y);

        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                w[j][i]=-sqrt((ant[i].x - tree[j].x)*(ant[i].x - tree[j].x)
                            + (ant[i].y - tree[j].y)*(ant[i].y - tree[j].y));
        km();
        if(m++) puts("");
        for(i=1; i<=n; i++) printf("%d\n", ytox[i]);
    }
    return 0;
}

 

posted @ 2013-09-13 01:52  Ramanujan  阅读(310)  评论(0编辑  收藏  举报