[poj3565]Ants

[poj3565]Ants

标签(空格分隔):二分图


描述

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


题意

有黑点和白点,需要连边,使任意黑点和白点都只被一条线段相连,任意两条线段不相交。


题解

线段不想交看上去很难处理,但是我们发现,如果两条线段相交,那么必定不是最好的情况。(如a1-b1,a2-b2相交,那么肯定不如a1-b2,a2-b1)所以我们直接给所有线段连上边,跑一个二分图最大匹配就行了。


Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<vector>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
inline int read()
{
	int sum=0,p=1;char ch=getchar();
	while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
	if(ch=='-')p=-1,ch=getchar();
	while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
	return sum*p;
}

const int maxn=400;

int n;
double a[maxn][maxn];
int mch[maxn];
double Lx[maxn],Ly[maxn];
bool T[maxn],S[maxn];
void init()
{
	ll x[maxn*2],y[maxn*2];
	REP(i,1,2*n)x[i]=read(),y[i]=read();
	REP(i,1,n)
		REP(j,1,n)a[j][i]=-sqrt((double)(x[j+n]-x[i])*(x[j+n]-x[i])+(y[j+n]-y[i])*(y[j+n]-y[i]));
}
const double eps=1e-9;
bool dfs(int u)
{
	S[u]=true;
    REP(v,1,n)
	{
		if(fabs(a[u][v]-(Lx[u]+Ly[v]))<eps && !T[v])
		{
			T[v]=true;
			if(!mch[v] || dfs(mch[v]))
			{
				mch[v]=u;
				return true;
			}
		}
	}
	return false;
}

void update()
{
	double c=1e30;
	REP(i,1,n)if(S[i])
		REP(j,1,n)if(!T[j])c=min(c,Lx[i]+Ly[j]-a[i][j]);
	REP(i,1,n)
	{
		if(S[i])Lx[i]-=c;
	 	if(T[i])Ly[i]+=c;
	}
}

void doing()
{
	REP(i,1,n)
	{
		mch[i]=Ly[i]=0;Lx[i]=-(1e30);
		REP(j,1,n)
		{
			Lx[i]=max(Lx[i],a[i][j]);
		}
	}
	REP(i,1,n)
	{
		while(1)
		{
			REP(j,1,n)S[j]=T[j]=0;
			if(dfs(i))break;
			else update();
		}
	}
	REP(i,1,n)cout<<mch[i]<<endl;
}

int main()
{
	while(scanf("%d",&n)==1)
	{
		init();
		doing();
	}
	return 0;
}

posted @ 2017-08-04 20:41  Deadecho  阅读(189)  评论(0编辑  收藏  举报