Uva1411 ants binary match

Source: Internet
Author: User
Previously, this problem was always done with the cost flow. Today I want to try the km. As a result, the previous km template was stuck. N ^ 4 is originally .... Make a decisive change. After all, I only used question a for that template ..... Previously, this type of problem had been decisive, and I felt that the time complexity of the km was too high. In addition, matrix storage is a method that I dislike very much. This question is two point sets with N points respectively. The line of the different point set cannot be intersecting. Ask about the final connection scheme. Convert to least-weight matching. The problem is resolved successfully. The matching result is that the shortest distance will not overlap. 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(1N(100 )--
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

#include<cstring>#include<algorithm>#include<cstdio>#include<iostream>#include<string>#include<cmath>using namespace std;const int maxn=555;#define INF 0x3f3f3f3fdouble w[maxn][maxn],x[maxn],y[maxn],slack[maxn];int prev_x[maxn],prev_y[maxn],matchy[maxn],par[maxn];int n;int ax[maxn],ay[maxn],bx[maxn],by[maxn];int mx[maxn];void update(int v){    matchy[v]=prev_y[v];    if(prev_x[matchy[v]]!=-2) update(prev_x[matchy[v]]);}bool find(int v){    for(int i=1;i<=n;i++)    {        if(prev_y[i]==-1)        {            if(slack[i]>x[v]+y[i]-w[v][i])            {                slack[i]=x[v]+y[i]-w[v][i];                par[i]=v;            }            if(x[v]+y[i]==w[v][i])            {                prev_y[i]=v;                if(matchy[i]==-1)                {                    update(i);                    return true;                }                if(prev_x[matchy[i]]!=-1) continue;                prev_x[matchy[i]]=i;                if(find(matchy[i])) return true;            }        }    }    return false;}double km(){    double m;    for(int i=1;i<=n;i++)    {        matchy[i]=-1;        y[i]=0;    }    for(int i=1;i<=n;i++)    {        x[i]=0;        for(int j=0;j<n;j++) x[i]=max(x[i],w[i][j]);    }    bool flag;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            prev_x[j]=prev_y[j]=-1;            slack[j]=INF;        }        prev_x[i]=-2;        if(find(i)) continue;        flag=false;        while(!flag)        {            m=INF;            for(int j=1;j<=n;j++)                if(prev_y[j]==-1)                    m=min(m,slack[j]);            for(int j=1;j<=n;j++)            {                if(prev_x[j]!=-1)                    x[j]-=m;                if(prev_y[j]!=-1)                    y[j]+=m;                else                    slack[j]-=m;            }            for(int j=1;j<=n;j++)                if(prev_y[j]==-1 && !slack[j])                {                    prev_y[j]=par[j];                    if(matchy[j]==-1)                    {                        update(j);                        flag=true;                        break;                    }                    prev_x[matchy[j]]=j;                    if(find(matchy[j]))                    {                        flag=true;                        break;                    }                }        }    }    double ans=0;    for(int i=1;i<=n;i++) ans+=w[matchy[i]][i];    return ans;}double dist(int i,int j){    return sqrt((ax[i]-bx[j])*(ax[i]-bx[j])+abs(ay[i]-by[j])*abs(ay[i]-by[j]));}int main(){    int f=0;    while(~scanf("%d",&n))    {        if(f) printf("\n");        f=1;        for(int i=1;i<=n;i++) scanf("%d%d",&ax[i],&ay[i]);        for(int i=1;i<=n;i++) scanf("%d%d",&bx[i],&by[i]);        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)                w[i][j]=-dist(i,j);        n;        km();        for(int i=1;i<=n;i++)            mx[matchy[i]]=i;        for(int i=1;i<=n;i++)            printf("%d\n",mx[i]);    }    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.