POJ 3565 Ants 最小权匹配
Posted 晓风微微
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3565 Ants 最小权匹配相关的知识,希望对你有一定的参考价值。
题目:
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 6852 | Accepted: 2153 | 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<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 110;
const double INF = 0xffffffffffff;
const double eps = 1e-6;
struct Node
{
double x,y;
}Dot1[MAXN],Dot2[MAXN];
double Dist(Node a,Node b)
{
return -sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int N,NX,NY;
double Map[MAXN][MAXN];
int link[MAXN];
double lx[MAXN],ly[MAXN],slack[MAXN];
int visx[MAXN],visy[MAXN];
int FindPath(int u)
{
visx[u] = 1;
for(int i = 1; i <= NY; ++i)
{
if(visy[i])
continue;
double temp = lx[u] + ly[i] - Map[u][i];
if(fabs(temp) <= eps)
{
visy[i] = 1;
if(link[i] == -1 || FindPath(link[i]))
{
link[i] = u;
return 1;
}
}
else
{
if(slack[i] > temp)
slack[i] = temp;
}
}
return 0;
}
void KM()
{
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(link,-1,sizeof(link));
for(int i = 1; i <= NX; ++i)
for(int j = 1; j <= NY; ++j)
if(Map[i][j] > lx[i])
lx[i] = Map[i][j];
for(int i = 1; i <= NX; ++i)
{
for(int j = 1; j <= NY; ++j)
slack[j] = INF;
while(1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(FindPath(i))
break;
double d = INF;
for(int j = 1; j <= NY; ++j)
if(!visy[j] && d > slack[j])
d = slack[j];
for(int j = 1; j <= NX; ++j)
if(visx[j])
lx[j] -= d;
for(int j = 1; j <= NY; ++j)
{
if(visy[j])
ly[j] += d;
else
slack[j] -= d;
}
}
}
}
struct point{
double x,y;
};
point a[150];
point b[150];
double distcount(point a,point b)
{
return -sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int ans[110];
int deal()
{
while (scanf("%d", &N)!=EOF) {
NX=NY=N;
memset(Map,0,sizeof(Map));
for (int i=1;i<=N;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
for (int i=1;i<=N;i++) scanf("%lf%lf",&b[i].x,&b[i].y);
for (int i = 1; i <= N; ++i)
for (int j = 1; j <=N; ++j){
Map[j][i]=distcount(a[i],b[j]);
}
KM();
for (int i=1;i<=N;i++) printf("%d\\n",link[i]);
}
return 0;
}
int main(){
deal();
return 0;
}
以上是关于POJ 3565 Ants 最小权匹配的主要内容,如果未能解决你的问题,请参考以下文章