poj 3565 Ants 最小权匹配
来源:互联网 发布:手机股票交易软件 知乎 编辑:程序博客网 时间:2024/06/13 01:41
Description
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 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
The first line of the input file 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 (−
Output
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.
Sample Input
5-42 5844 867 2899 34-13 -59-47 -4486 7468 -75-68 6099 -60
Sample Output
42351
Source
题意:给出n个白点和n个黑点的坐标,要用n条不想交的线段把它们连接,线段端点为一个白点和一个黑点。输出n行,第i行为第i个白点所连接的黑点编号。
思路:最小权匹配。我们很容易看出这是个二分图,每个白点对应一个X节点,每个对应一个Y节点,每个黑点和每个白点相连,权值等于二者的欧几里得距离的负
值,那么求出当前图的最大权匹配就是解。若有两条线段a1-b1,a2-b2相交,那么d(a1,b1)+d(a2,b2)>d(a1,b2)+d(a2,b1),所以应该求原图的最小权匹配,即
当前图的最大权匹配,详见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int MAXN=100+50;const int inf=0x3fffffff;const double eps=1e-10;int n;int S[MAXN],T[MAXN],ans[MAXN],link[MAXN];double lx[MAXN],ly[MAXN],w[MAXN][MAXN];struct node{ int x,y;}B[MAXN],W[MAXN];void update(){ double x=inf; for(int i=1;i<=n;i++) if(S[i]){ for(int j=1;j<=n;j++) if(!T[j]) x=min(x,lx[i]+ly[j]-w[i][j]); } for(int i=1;i<=n;i++){ if(S[i]) lx[i]-=x; if(T[i]) ly[i]+=x; }}int path(int u){ S[u]=1; for(int v=1;v<=n;v++) if(!T[v] && fabs(lx[u]+ly[v]-w[u][v])<eps){ T[v]=1; if(link[v]==-1 || path(link[v])) { link[v]=u; return 1; } } return 0;}void KM(){ for(int i=1;i<=n;i++){ lx[i]=-inf,ly[i]=0; for(int j=1;j<=n;j++) lx[i]=max(lx[i],w[i][j]); } for(int i=1;i<=n;i++){ while(1){ for(int j=1;j<=n;j++) S[j]=T[j]=0; if(path(i)) break; else update(); } }}int main(){ //freopen("text.txt","r",stdin); while(~scanf("%d",&n)){ for(int i=1;i<=n;i++) scanf("%d%d",&B[i].x,&B[i].y); for(int i=1;i<=n;i++) scanf("%d%d",&W[i].x,&W[i].y); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) w[i][j]=-sqrt((double)(B[i].x-W[j].x)*(B[i].x-W[j].x)+(double)(B[i].y-W[j].y)*(B[i].y-W[j].y)); memset(link,-1,sizeof(link)); KM(); for(int i=1;i<=n;i++) ans[link[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",ans[i]); } return 0;}
- poj 3565 Ants 最小权匹配
- Ants (poj 3565 最小权匹配)
- POJ 3565 Ants (最小权完美匹配 KM算法)
- POJ 3565 Ants(二分图最小权完备匹配)
- POJ 3565 Ants(匹配? 计算几何!)
- POJ 3565 最小权匹配
- POJ 3686 最小权匹配
- POJ 2400 最小权匹配
- poj 2400 最小权匹配+匹配输出
- POJ 3565 Ants
- poj 3565 Ants KM
- POJ 3565 Ants
- POJ 3565 - Ants
- POJ 3565 Ants
- POJ 3545 Ants(最佳二分图匹配)
- [省选前题目整理][LA 4043]Ants(二分图最小权匹配)
- uva 1411 Ants (权值和最小的完美匹配---KM算法)
- 分治法,最小权完美匹配(巨人和鬼,Ants,uva 1411)
- DRP学习中的模型进化
- xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)
- Ancient Cipher(古老的加密(判断译文和密文是否可能对应))
- 怎样理解分页作业是一维的,分段作业是二维的
- iphone开发笔记
- poj 3565 Ants 最小权匹配
- LeetCode2.1.17 @ Plus One 数组数字加一 D1F2
- windows下使用for 和perl命令批量修改多个文件内容
- UVA 1482 - Playing With Stones(SG打表规律)
- Android实现异步处理 -- HTTP请求为例
- swift不同类型 相加
- MYSQL 表中汉字写入或字段赋值时乱码情况排误
- MFC内部组织原理结构
- Windows下配置Ruby on Rails开发环境