POJ 3565 KM算法
来源:互联网 发布:mac怎么自动关机 编辑:程序博客网 时间:2024/05/25 19:55
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
5-42 5844 867 2899 34-13 -59-47 -4486 7468 -75-68 6099 -60
Sample Output
42153
Source
#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#define inf 99999999.0using namespace std;struct Point{double x;double y;};Point ant[105];Point tree[105];double map[105][105];int link[105];double lx[105];double ly[105];bool x[105];bool y[105];int n;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 init(){int i,j;for(i=0;i<105;i++){lx[i]=inf;ly[i]=0;link[i]=-1;}for(i=0;i<105;i++)for(j=0;j<105;j++)map[i][j]=0;}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;elsereturn 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;double d;init();scanf("%d",&n);for(i=1;i<=n;i++)scanf("%lf%lf",&ant[i].x,&ant[i].y);for(i=1;i<=n;i++)scanf("%lf%lf",&tree[i].x,&tree[i].y);for(i=1;i<=n;i++){for(j=1;j<=n;j++){map[i][j]=dist(ant[i],tree[j]);//printf("%lf ",map[i][j]);}//printf("/n");}for(k=1;k<=n;k++){while(1){memset(x,0,sizeof(x));memset(y,0,sizeof(y));if(find(k))break;d=inf;for(i=1;i<=n;i++)if(x[i])for(j=1;j<=n;j++)if(!y[j]&&(lx[i]+ly[j]-map[i][j]<d))d=lx[i]+ly[j]-map[i][j];for(i=1;i<=n;i++)if(x[i])lx[i]=lx[i]-d;for(i=1;i<=n;i++)if(y[i])ly[i]=ly[i]+d;}}double uva=0.0;for(i=1;i<=n;i++)for(j=1;j<=n;j++){if(link[j]==i){printf("%d/n",j);break;}}return 0;}
- POJ 3565 KM算法
- Ants - POJ 3565 KM算法
- poj 3565 Ants KM算法
- POJ 3565 Ants (证明+KM算法)
- POJ 3565 KM算法 题库221
- poj 3565 Ants (KM算法)
- poj 2195 KM算法
- poj 2195(KM算法模板)
- poj 3565 Ants KM
- POJ 3565 Ants (最小权完美匹配 KM算法)
- sicily 1013 poj 2195 km算法
- poj 2195 KM算法(完备匹配)
- POJ 3686 The Windy's KM算法
- 重做 poj 2195(KM算法)
- poj 2195 Going Home km算法
- 二分图KM算法 POJ 2195
- Going Home - POJ 1295 KM算法
- The Windy's - POJ 3686 KM算法
- ViewState,PagedDataSource实现分页功能
- 谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词
- ubuntu 下 驱动开发环境
- socket在windows下和linux下的区别
- typedef的四个用途和两个陷阱
- POJ 3565 KM算法
- 《编程人生》--与编程大师们的对话
- 摘抄的经验
- 需求-开发-售后-需求是一条财富信息链
- 搜索速度问题:本地索引与hdfs平台索引,中文搜索与英文搜索速度不一致
- linux下USB驱动开发
- 单链表逆向 c
- Jni中使用多态
- Repeater控件的ItemCreated事件实现权限与ItemCommand事件