Ants (poj 3565 最小权匹配)

11 篇文章 0 订阅

Language:
Ants
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5585 Accepted: 1732 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

Source



题意:告诉n个蚂蚁和n棵苹果树,问每只蚂蚁去哪个苹果树使得所有路径不相交。

思路:最小权匹配能保证路径不相交。

代码:

#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 105;
const int MAXM = 200010;
const int N = 1005;

struct Node
{
    double x,y;
}ant[MAXN],apple[MAXN];

int nx,ny;
double g[MAXN][MAXN];
int linker[MAXN];
double lx[MAXN],ly[MAXN];
double slack[MAXN];
bool visx[MAXN],visy[MAXN];

bool DFS(int x)
{
    visx[x]=true;
    for (int y=0;y<ny;y++)
    {
        if (visy[y]) continue;
        double tmp=lx[x]+ly[y]-g[x][y];
        if (fabs(tmp)<eps)
        {
            visy[y]=true;
            if (linker[y]==-1||DFS(linker[y]))
            {
                linker[y]=x;
                return true;
            }
        }
        else if (slack[y]>tmp) slack[y]=tmp;
    }
    return false;
}

void KM()
{
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for (int i=0;i<nx;i++)
    {
        lx[i]=-INF;
        for (int j=0;j<ny;j++)
        {
            if (g[i][j]>lx[i])
                lx[i]=g[i][j];
        }
    }
    for (int x=0;x<nx;x++)
    {
        for (int i=0;i<ny;i++)
            slack[i]=INF;
        while (true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if (DFS(x)) break;
            double d=INF;
            for (int i=0;i<ny;i++)
                if (!visy[i]&&d>slack[i])
                    d=slack[i];
            for (int i=0;i<nx;i++)
                if (visx[i])
                    lx[i]-=d;
            for (int i=0;i<ny;i++){
                if (visy[i]) ly[i]+=d;
                else slack[i]-=d;
            }
        }
    }
    for (int i=0;i<nx;i++)
        printf("%d\n",linker[i]+1);
}

double Dis(Node a,Node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j;
    while (~scanf("%d",&nx))
    {
        ny=nx;
        for (i=0;i<nx;i++)
            scanf("%lf%lf",&ant[i].x,&ant[i].y);
        for (i=0;i<nx;i++)
            scanf("%lf%lf",&apple[i].x,&apple[i].y);
        for (i=0;i<nx;i++)
            for (j=0;j<nx;j++)
                g[i][j]=-1.0*Dis(apple[i],ant[j]);
        KM();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值