LA-4043
来源:互联网 发布:java闰年判断case 编辑:程序博客网 时间:2024/04/28 00:07
4043 - Ants
Time limit: 3.000 seconds
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 andn 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 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 has several dataset. The first line of each dataset contains a single integer numbern (1n100) – the number of ant colonies and apple trees. It is followed by n lines describing n ant colonies, followed byn lines describing n apple trees. Each ant colony and apple tree is described by a pair of integer coordinatesx and y (- 10000x,y
For each dataset, write to the output file n lines with one integer number on each line. The number written oni -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.
5
-42 58
44 86
7 28
99 34
-13 -59
-47 -44
86 74
68 -75
-68 60
99 -60
4
2
1
5
3
KM算法,最佳完美匹配。
#include <cstring>#include <cmath>#include <cstdio>#include <iostream>using namespace std;const int MAXN=2147483647;int n,lx[201],ly[201],slack[201],w[201][201],fy[201];bool mx[201],my[201];struct thing{ int x,y;} pointw[101],pointb[101];int sqr(int x){ return x*x;}int find(int x){ mx[x]=true; for(int i=1;i <= n;i++) { if(my[i]) continue; int a=lx[x]+ly[i]-w[x][i]; if(a == 0) { my[i]=true; if(!fy[i] || find(fy[i])) { fy[i]=x; return true; } } else slack[i]=min(slack[i],a); } return false;}void km(){ memset(fy,0,sizeof(fy)); memset(ly,0,sizeof(ly)); for(int i=1;i <= n;i++) { lx[i]=-MAXN; for(int j=1;j <= n;j++) lx[i]=max(lx[i],w[i][j]); } for(int i=1;i <= n;i++) { for(int i=1;i <= n;i++) slack[i]=MAXN; while(true) { memset(mx,0,sizeof(mx)); memset(my,0,sizeof(my)); if(find(i)) break; int a=MAXN; for(int i=1;i <= n;i++) if(!my[i]) a=min(a,slack[i]); for(int i=1;i <= n;i++) if(mx[i]) lx[i]-=a; for(int i=1;i <= n;i++) if(my[i]) { ly[i]+=a; } else { slack[i]-=a; } } } for(int i=1;i <= n;i++) cout<<fy[i]<<endl;}int main(){ bool flag=false; while(cin>>n) { if(flag) cout<<endl; flag=true; for(int i=1;i <= n;i++) cin>>pointb[i].x>>pointb[i].y; for(int i=1;i <= n;i++) cin>>pointw[i].x>>pointw[i].y; for(int i=1;i <= n;i++) for(int j=1;j <= n;j++) w[i][j]=-sqr(pointw[i].x-pointb[j].x)-sqr(pointw[i].y-pointb[j].y); km(); }}
- LA-4043
- LA 4043
- LA 4043 Ants
- la la la, la la la
- LA 4043 - Ants 求完美匹配
- LA 4043 Ants (最佳完美匹配)
- LA 4043 Ants 二分图完美匹配
- LA - 4043 - Ants(二分图最佳完美匹配)
- LA 3942
- LA 4670
- LA 3135
- LA 3644
- LA 3027
- LA 5059
- LA 3644
- La 3942
- LA 3126
- LA 4329
- javascript中Map代替循环
- 非常全面的ioS和Android学习路线图
- JPA Hibernate 和数据库 的 锁机制
- debian下使用dpkg来安装/卸载deb包
- USACO 6.2.3 shaping regions 矩形切割
- LA-4043
- context
- 自定义ScrollView
- 备份系列——Exp/Imp基本使用指南
- Win7下安装Python图像处理库PIL、pytesser、tesseract进行验证码识别
- 修改oracle database中user的密码遇到ORA-28003和ORA-20001
- Spring整合RabbitMQ进行消息队列开发
- AIDL随写
- Zookeeper异常:FAILED TO WRITE PID与Permission denied