poj 2565 Ants (KM+思维)
来源:互联网 发布:淘宝店铺号码怎么查找 编辑:程序博客网 时间:2024/04/29 11:36
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
42153
Source
题意:
给你n个黑点和n个白点。叫你找出一种连点的方法使得每一白点连一个黑点。切连线不与其它连线相交。
思路:
看上去跟想几何问题跟KM没什么关系。但确实是KM题。求连线的最短距离就行了。因为在连线距离最小的条件下。不会有两条直线相交的情况。画个图就知道(三角形两边之和大于第三边)。需要稍稍的转换下。两点间的价值为距离的负数。还有特别注意答案要求输出每一个C对应的A。所以建边时要注意left[i]代表什么!鄙人就为这个WA了好几发。TT。
详细见代码:
#include<algorithm>#include<iostream>#include<string.h>#include<sstream>#include<stdio.h>#include<math.h>#include<vector>#include<string>#include<queue>#include<set>#include<map>//#pragma comment(linker,"/STACK:1024000000,1024000000")using namespace std;const int INF=0x3f3f3f3f;const double eps=1e-6;const double PI=acos(-1.0);const int maxn=110;//typedef __int64 ll;int le[maxn],n;double lx[maxn],ly[maxn],slack[maxn],w[maxn][maxn],ax[maxn],ay[maxn],bx[maxn],by[maxn];bool visx[maxn],visy[maxn];double dist(double x1,double y1,double x2,double y2){ double x=x1-x2,y=y1-y2; return sqrt(x*x+y*y);}bool match(int x){ int y; double tp; visx[x]=true; for(y=1;y<=n;y++) { if(visy[y]) continue; tp=lx[x]+ly[y]-w[x][y]; if(tp<eps) { visy[y]=true; if(!le[y]||match(le[y])) { le[y]=x; return true; } } else slack[y]=min(slack[y],tp); } return false;}void update(){ int i; double d; for(i=1,d=INF;i<=n;i++) if(!visy[i]) d=min(d,slack[i]); for(i=1;i<=n;i++) { if(visx[i]) lx[i]-=d; if(visy[i]) ly[i]+=d; else slack[i]-=d; }}void KM(){ int i,j,x; memset(le,0,sizeof le); for(i=1;i<=n;i++) { lx[i]=-INF;//注意这里!! ly[i]=0; for(j=1;j<=n;j++) lx[i]=max(lx[i],w[i][j]); } for(x=1;x<=n;x++) { for(i=1;i<=n;i++) slack[i]=INF; while(1) { for(i=1;i<=n;i++) visx[i]=visy[i]=false; if(match(x)) break; else update(); } }}int main(){ int i,j; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) scanf("%lf%lf",&ax[i],&ay[i]); for(i=1;i<=n;i++) scanf("%lf%lf",&bx[i],&by[i]); for(i=1;i<=n;i++) for(j=1;j<=n;j++) w[j][i]=-dist(ax[i],ay[i],bx[j],by[j]);//这里特别注意!由于答案输出的关系 KM(); for(i=1;i<=n;i++) printf("%d\n",le[i]); } return 0;}
- poj 2565 Ants (KM+思维)
- poj 3565 Ants KM
- Ants - POJ 3565 KM算法
- POJ 3565 Ants (KM)
- poj 3565 Ants KM算法
- poj 1852 Ants 【思维】
- poj 1852 Ants (思维)
- poj-1852-Ants【思维】
- POJ 3565 Ants(计算几何,KM)
- POJ 3565 Ants (证明+KM算法)
- poj 3565 Ants (KM算法)
- POJ 1852 Ants(思维)
- poj 1852 ants 思维题
- POJ 1852 Ants 思维题
- 简单思维题 poj 1852 Ants
- POJ 1852 Ants (思维技巧 + 贪心)
- poj -1852 ants (思维题)
- POJ 3565 Ants (最小权完美匹配 KM算法)
- ioctl接口
- js学习小结(三)2014.4.18-外边距合并,负边距和浏览器渲染,盒模型
- Java中内存中的Heap(堆)、Stack(栈)与程序运行的关系
- My97DatePicker日历实现开始日期小于结束日期验证
- seek接口
- poj 2565 Ants (KM+思维)
- Java知识总结——LinkedList和ArrayList的区别
- OPENCV中滑动条的使用
- The Right Way to Swizzle in Objective-C
- sql2005的northwind
- select、pselect、poll和epoll的区别
- 为何会产生warning: no newline at end of file?
- poll接口
- 基于Qt的语音识别功能