POJ 3565 Ants
2015-12-31 15:36
197 查看
Ants
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 (−10 000 ≤ x, y ≤ 10 000) 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
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
Sample Output
最近要做关于二分图匹配研究的毕设,就随便挑了道题目做做。题目就是n个蚂蚁,和n个树木,求出一种让蚂蚁和树木都有各自的对应的直线连接,并且各自不能相交。题目故意不说是所有直线距离加起来最短,就是为了不让我们发现其实最短的就是一种解。因为一旦有相交就肯定不是距离最优解。所以此题用KM算法即可解出。
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 5731 | Accepted: 1781 | Special Judge |
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 (−10 000 ≤ x, y ≤ 10 000) 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
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 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算法即可解出。
#include<iostream> #include<cstdio> #include<cstdlib> #include<ctime> #include<string> #include<cstring> #include<algorithm> #include<fstream> #include<queue> #include<stack> #include<vector> #include<cmath> #include<iomanip> #define rep(i,n) for(i=1;i<=n;i++) #define MM(a,t) memset(a,t,sizeof(a)) #define INF 1e9 typedef long long ll; #define mod 1000000007 using namespace std; struct Point{ double x,y; }; int n; Point ant[102],tree[102]; double map[102][102]; int link[105]; double lx[105]; double ly[105]; bool x[105]; bool y[105]; double dist(Point a,Point b) { return -sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void setup(){ int i,j; rep(i,n){ lx[i]=INF; ly[i]=0; link[i]=-1; } } bool judge(int v,int i) { double esp; esp=0.0001; double r=lx[v]+ly[i]; double z=map[v][i]; if(abs(r-z)<=esp) return true; else return false; } bool find(int v) { int i; x[v]=true; for(i=1;i<=n;i++) { if(!y[i]&&judge(v,i)) { y[i]=true; if(link[i]==-1||find(link[i])) { link[i]=v; return true; } } } return false; } int main(){ int i,j,k; while(scanf("%d",&n)!=EOF){ rep(i,n) scanf("%lf%lf",&ant[i].x,&ant[i].y); rep(i,n) scanf("%lf%lf",&tree[i].x,&tree[i].y); rep(i,n) rep(j,n) map[i][j]=dist(ant[i],tree[j]); setup(); rep(k,n){ double d; while(1){ MM(x,0); MM(y,0); if(find(k)) break; d=INF; rep(i,n) if(x[i]) rep(j,n) if(!y[j]&&(lx[i]+ly[j]-map[i][j]<d)) d=lx[i]+ly[j]-map[i][j]; rep(i,n) if(x[i]) lx[i]=lx[i]-d; rep(i,n) if(y[i]) ly[i]=ly[i]+d; } } rep(i,n) rep(j,n) if(link[j]==i){ printf("%d\n",j); break; } } return 0; }
相关文章推荐
- [Java][MyBatis]物理分页实现
- 本地电脑连接阿里云RDS Mysql数据库问题(10038错误)
- POSIX标准小结
- php7 对返回值的限定
- PowerBuilder
- 类加载器--深入探讨 Java 类加载器
- 利用Jmeter进行分布式性能测试
- mysql误操作引起的问题
- 【STL】list
- githug游戏--通关文档
- java I/O 流总结学习
- MONGODB(三)——Java操作Mongo
- Android BitmapMesh使图片扭曲
- 带有HA功能的Hadoop Client端RPC实现原理与代码分析
- Hive 分析函数lead、lag实例应用
- 深入理解mysql event事件调度器
- 如何用AngularJS构建管理后台
- 自定义UITableViewCell上的按钮点击事件处理
- 关于CSDN无法编辑自己简介的问题
- IOS TableView解析