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;
    }
    



  • 相关阅读:
    js 或者 element-ui 将年月日时分秒转换为时间戳
    element-ui 设置table 表头多列显示
    element-ui table 给表头添加icon,以及hover上去的提示文字
    js 获取本周开始结束时间,本月开始结束时间等....
    element-ui Table 翻页后记忆之前勾选
    element-ui 上传图片或视频时,先回显在上传
    element-ui Upload 上传获取当前选择的视频时长
    element-ui 自定义 Upload 上传进度条
    Sqoop(二)常用命令及常数解析
    使用IDEA构建Spring Boot项目简单实例
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/3853666.html
Copyright © 2011-2022 走看看