zoukankan      html  css  js  c++  java
  • 【UVA 1411】 Ants (KM)

    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 the 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 ≤ 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 (−10000 ≤ x, y ≤ 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-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

    【题意】

      给出平面上n个白点n个黑点,要求两两配对,且配对所连线段没有交点。

    【分析】

      处理不相交的方法,用点的欧几里德距离作为边权进行KM,那么对于ABCD四个点来说,一定取的是不相交那一种。

    因为是有小数点的KM,于是我又打了一下。。。

    这种要证明有界性的算法真的打错了一点点就好容易RE,又不会调,(模拟真是太难了,不如肉眼找错= =)

    代码如下:

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<cmath>
      8 using namespace std;
      9 #define Maxn 110
     10 #define Maxm 10010
     11 #define INF 0xfffffff
     12 
     13 struct node
     14 {
     15     int x,y,next;
     16     double c;
     17 }t[Maxm];int len;
     18 int first[Maxn];
     19 
     20 double mymax(double x,double y) {return x>y?x:y;}
     21 double mymin(double x,double y) {return x<y?x:y;}
     22 double fabs(double x) {return x<0?-x:x;}
     23 
     24 void ins(int x,int y,double c)
     25 {
     26     t[++len].x=x;t[len].y=y;t[len].c=-c;
     27     t[len].next=first[x];first[x]=len;
     28 }
     29 
     30 int n;
     31 double lx[Maxn],ly[Maxn],slack[Maxn];
     32 int match[Maxn];
     33 bool visx[Maxn],visy[Maxn];
     34 
     35 int ax[Maxn],ay[Maxn],bx[Maxn],by[Maxn];
     36 
     37 bool ffind(int x)
     38 {
     39     visx[x]=1;
     40     for(int i=first[x];i;i=t[i].next) if(!visy[t[i].y])
     41     {
     42         int y=t[i].y;
     43         if(fabs(lx[x]+ly[y]-t[i].c)<0.00001)
     44         {
     45             visy[y]=1;
     46             if(!match[y]||ffind(match[y]))
     47             {
     48                 match[y]=x;
     49                 return 1;
     50             }
     51         }
     52         else slack[y]=mymin(slack[y],lx[x]+ly[y]-t[i].c);
     53     }
     54     return 0;
     55 }
     56 
     57 void solve()
     58 {
     59     memset(match,0,sizeof(match));
     60     for(int i=1;i<=n;i++)
     61     {
     62         ly[i]=0;
     63         lx[i]=-INF;
     64         for(int j=first[i];j;j=t[j].next) lx[i]=mymax(lx[i],t[j].c);
     65     }
     66     int i;
     67     for(i=1;i<=n;i++)
     68     {
     69         for(int j=1;j<=n;j++) slack[j]=INF;
     70         while(1)
     71         {
     72             memset(visx,0,sizeof(visx));
     73             memset(visy,0,sizeof(visy));
     74             if(ffind(i)) break;
     75             double delta=INF;
     76             for(int j=1;j<=n;j++) if(!visy[j])
     77                 delta=mymin(delta,slack[j]);
     78             if(fabs(delta-INF)<0.00001) return;
     79             for(int j=1;j<=n;j++)
     80             {
     81                 if(visx[j]) lx[j]-=delta;
     82                 if(visy[j]) ly[j]+=delta;
     83                 else if(fabs(slack[j]-INF)>0.00001) slack[j]-=delta; 
     84             }
     85         }
     86     }
     87 }
     88 
     89 int main()
     90 {
     91     while(scanf("%d",&n)!=EOF)
     92     {
     93         for(int i=1;i<=n;i++) scanf("%d%d",&bx[i],&by[i]);
     94         for(int i=1;i<=n;i++) scanf("%d%d",&ax[i],&ay[i]);
     95         // for(int i=1;i<=n;i++) scanf("%d%d",&bx[i],&by[i]);
     96         len=0;
     97         memset(first,0,sizeof(first));
     98         for(int i=1;i<=n;i++)
     99          for(int j=1;j<=n;j++)
    100          {
    101              double d=(double)((ax[i]-bx[j])*(ax[i]-bx[j])+(ay[i]-by[j])*(ay[i]-by[j]));
    102              d=sqrt(d);
    103              ins(i,j,d);
    104          }
    105          solve();
    106          for(int i=1;i<=n;i++) printf("%d
    ",match[i]);
    107     }
    108     return 0;
    109 }
    [UVA 1411]

    2016-10-27 14:50:29

  • 相关阅读:
    Android基础之项目结构分析
    串口调试,提示the given port name does not start with COM/com异常解决办法,,发现是打印机在搞怪
    C# 通过URL获取图片并显示在PictureBox上的方法
    学习资料集合
    C#语音朗读文本 — TTS的实现
    SQL SERVER 2008安装错误(is not a valid login or you do have permission)
    函数调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。
    SerialPort使用
    Javascript函数的几种写法
    JS验证图片格式和大小并预览
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/6003933.html
Copyright © 2011-2022 走看看