UVALive 4043 Ants(二分图匹配,最大匹配,4级)

A - Ants
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: 

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 <tex2html_verbatim_mark>ant colonies and n <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>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}<tex2html_verbatim_mark>

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 <tex2html_verbatim_mark>(1$ \le$n$ \le$100) <tex2html_verbatim_mark>-- the number of ant colonies and apple trees. It is followed by n <tex2html_verbatim_mark>lines describing n <tex2html_verbatim_mark>ant colonies, followed by n <tex2html_verbatim_mark>lines describing n <tex2html_verbatim_mark>apple trees. Each ant colony and apple tree is described by a pair of integer coordinates x <tex2html_verbatim_mark>and y <tex2html_verbatim_mark>(- 10000$ \le$x, y$ \le$10000) <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>lines with one integer number on each line. The number written on i <tex2html_verbatim_mark>-th line denotes the number (from 1 to n <tex2html_verbatim_mark>) of the apple tree that is connected to the i i<tex2html_verbatim_mark>-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

思路:最近的一定是可行的。因此就是最近的匹配。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int mm=110;
 8 const double mx=1e-6;
 9 const double oo=1e40;
10 double dis[mm][mm],lx[mm],ly[mm],slack[mm];
11 int Left[mm];
12 bool S[mm],T[mm];
13 class node
14 {
15   public:double x,y;
16 }ant[mm],apple[mm];
17 int n;
18 double get_dis(int a,int b)
19 {
20   return sqrt(((apple[a].x-ant[b].x)*(apple[a].x-ant[b].x))+(apple[a].y-ant[b].y)*(apple[a].y-ant[b].y));
21 }
22 double fabs(double x)
23 {
24   if(x<0)return -x;
25   return x;
26 }
27 bool match(int u)
28 { S[u]=1;
29   double tmp;
30   for(int v=0;v<n;++v)
31   { tmp=lx[u]+ly[v]-dis[u][v];
32     if(!T[v])
33     {if(fabs(tmp)<mx)
34       { T[v]=1;
35         if(Left[v]==-1||match(Left[v]))
36         {
37         Left[v]=u;return 1;
38         }
39       }else slack[v]=min(slack[v],tmp);
40     }
41 
42   }
43   return 0;
44 }
45 void update()
46 {
47   double ret=oo;
48   for(int i=0;i<n;++i)
49     if(!T[i])
50     ret=min(ret,slack[i]);
51   for(int i=0;i<n;++i)
52   {
53     if(S[i])lx[i]-=ret;
54     if(T[i])ly[i]+=ret;
55   }
56 }
57 void KM()
58 {
59   for(int i=0;i<n;++i)
60   {
61     lx[i]=-oo;ly[i]=0.0;Left[i]=-1;
62     for(int j=0;j<n;++j)
63       lx[i]=max(lx[i],dis[i][j]);
64   }
65   for(int i=0;i<n;++i)
66   { for(int j=0;j<n;++j)
67     slack[j]=oo;
68     while(1)
69     {
70     memset(S,0,sizeof(S));
71     memset(T,0,sizeof(T));
72     if(match(i))break;
73     else update();
74     }
75   }
76 }
77 int main()
78 { int cas=0;
79   while(~scanf("%d",&n))
80   {
81     if(cas)printf("\n");
82     ++cas;
83     for(int i=0;i<n;++i)
84       scanf("%lf%lf",&ant[i].x,&ant[i].y);
85     for(int i=0;i<n;++i)
86       scanf("%lf%lf",&apple[i].x,&apple[i].y);
87     for(int i=0;i<n;++i)
88       for(int j=0;j<n;++j)
89       dis[i][j]=-get_dis(i,j);
90       KM();
91       for(int i=0;i<n;++i)
92         printf("%d\n",Left[i]+1);
93   }
94   return 0;
95 }
View Code

 

posted @ 2013-07-27 12:42  剑不飞  阅读(304)  评论(0编辑  收藏  举报