POJ 3565 KM算法

原创 2011年01月15日 23:38:00
Ants
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1842 Accepted: 527 Special Judge

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 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

Source

可以用匹配来写,因为在最小权匹配的时候所有线段一定不相交!!!
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define inf 99999999.0

using 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;
	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;
	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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ3565(最大权匹配)

大白351例题23和这题类似。 最大权匹配一定不存在相交。 需要对距离取反。 #include #include #include #include #include #include...

二分图最佳匹配 KM算法 Hdu2255奔小康赚大钱 + Poj 3565 Ants

理论:http://blog.sina.com.cn/s/blog_691ce2b701016reh.html http://philoscience.iteye.com/blog/1754498 ...

POJ 3565 Ants (最小权完美匹配 KM算法)

POJ 3565 Ants (最小权匹配 KM算法)

km算法+slack优化 poj3565

km算法在百度上讲的很清楚讲一些针对这道题的有n只蚂蚁要吃n个苹果,求总路径和最小,且不交叉的方案网上有很多人说是计算几何题其实,研究一下题意可以得出很重要的性质————最优方案一定不交叉。根据三角形...

poj 3565 Ants KM算法

题目:http://poj.org/problem?id=3565 题意:给你n个蚁群和n个苹果树,用两个数表示其坐标,把每个蚁群对应一棵苹果树,且所有的蚁群和其对应的苹果树的连线不能相交。输出每个蚁...

POJ_3565 Ants KM匹配

http://poj.org/problem?id=3565 题意: 有N只ants和N棵tree,要求一种ants和tree的匹配方案,使得ant和tree之间的匹配线段互不相交。 思路: ...

poj 3565 Ants KM

题意: 给n只蚂蚁和n课苹果树的坐标,要让每只蚂蚁去一棵苹果树,路线不能重复,求一种可行方案。 分析: 当某种匹配可行时蚂蚁所走的距离和是最小的,可以直接用KM算法求二分图最小权值匹配。还有一种做法...

hdu2255+poj3565(km模板题)

hdu2255 奔小康赚大钱

poj3565 ants(KM)

题目链接:http://poj.org/problem?id=3565 题意: 有 N 个蚂蚁,N 个苹果,要在每个蚂蚁和一个相应的苹果之间连边,问如何给蚂蚁分配苹果,可以使这些边不相交。分析: ...

poj-3565 Ants(计算几何+调整算法)

题目链接:点击打开链接 Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)