zoukankan      html  css  js  c++  java
  • UVALive 4043 Ants


    KM   构图求最小权值匹配

    保证最小的权值,所连的边一定是能够不相交的.

    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

    []   [Go Back]   [Status]  

    Description

    Download as PDF

    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.

    epsfbox{p4043.eps}

    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 number n(1$ le$n$ le$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(- 10000$ le$xy$ le$10000) 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 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 i -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

    []   [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
    ",linker[i]+1);
            }
        }
        return 0;
    }
    



  • 相关阅读:
    火炬之光模型导出(Unity载入火炬之光的模型)
    树的左旋与右旋
    javaEE开发之导出excel工具类
    STL algorithm算法is_permutation(27)
    学做衣服论坛 -服装DIY教程,缤纷服装网,裁剪教程,家用缝纫机,买布料
    傲娇_百度百科
    《失败不是成功之母》阅读理解
    失败是不是成功之母
    正则表达式多语种的web版本
    date tod = boost::gregorian::day_clock::local_day(); //当前日期
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/3853666.html
Copyright © 2011-2022 走看看