Uvalive 4043 ants

Source: Internet
Author: User


KM Diagram for minimum weight matching

To ensure the minimum weight, the connected edge must be non-intersecting.

Ants
Time limit:3000 Ms   Memory limit:Unknown   64bit Io format:% LLD & % LlU

[Submit] [Go Back] [Status]

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 coordinatesNAnt colonies andNApple 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 wowould like to connect each ant colony to a single apple tree so that allNRoutes 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

Input has several dataset. The first line of each dataset contains a single integer numberN(1N100) -- the number of ant colonies and apple trees. It is followedNLines describingNAnt colonies, followedNLines describingNApple Trees. Each ant colony and apple tree is described by a pair of integer coordinatesXAndY(-10000X,Y10000) 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

For each dataset, write to the output fileNLines with one integer number on each line. The number written onI-Th line denotes the number (from 1N) Of the apple tree that is connected toII-th ant colony. Print a blank line between datasets.

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

Northeastern Europe 2007-2008

[Submit] [Go Back] [Status]



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn=220;const double INF=999999999999999.;const double eps=1e-5;struct Dian{    double x,y;}white[maxn*maxn],black[maxn*maxn];int n;double g[maxn][maxn];int linker[maxn*maxn];double lx[maxn*maxn],ly[maxn*maxn];double slack[maxn*maxn];bool visx[maxn*maxn],visy[maxn*maxn];bool dfs(int x){    visx[x]=true;    for(int y=0;y<n;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;}int KM(){    memset(linker,-1,sizeof(linker));    memset(ly,0,sizeof(ly));    for(int i=0;i<n;i++)    {        lx[i]=-INF;        for(int j=0;j<n;j++)            if(g[i][j]>lx[i])                lx[i]=g[i][j];    }    for(int x=0;x<n;x++)    {        for(int i=0;i<n;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<n;i++)                if(!visy[i]&&d>slack[i])                    d=slack[i];            for(int i=0;i<n;i++)                if(visx[i])                    lx[i]-=d;            for(int i=0;i<n;i++)                if(visy[i])                    ly[i]+=d;                else                    slack[i]-=d;        }    }}double Dist(Dian a,Dian b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){    bool first=false;    while(scanf("%d",&n)!=EOF)    {        if(first) putchar(10);        else first=true;        for(int i=0;i<n;i++)        {            double a,b;            scanf("%lf%lf",&a,&b);            white[i]=(Dian){a,b};        }        for(int i=0;i<n;i++)        {            double a,b;            scanf("%lf%lf",&a,&b);            black[i]=(Dian){a,b};        }        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                double t=Dist(black[i],white[j]);                g[i][j]=-t;            }        }        KM();        for(int i=0;i<n;i++)        {            printf("%d\n",linker[i]+1);        }    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.