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

[poj3565]Ants的更多相关文章

  1. poj3565 Ants km算法求最小权完美匹配,浮点权值

    /** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...

  2. POJ3565 Ants 和 POJ2195 Going Home

    Ants Language:Default Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7975 Accepted: ...

  3. ACM学习历程—POJ3565 Ants(最佳匹配KM算法)

    Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees ...

  4. POJ3565 Ants (不相交线)

    那请告诉我 A - D  B - C  和  A - C  B - D 那个的和小 显然是A - C  B - D  (可以根据四边形 对角线大于对边之和) 然后 求的答案是不是就一定是不相交的 就是 ...

  5. [poj3565] Ants (二分图带权匹配)

    传送门 Description 年轻自然主义者比尔在学校研究蚂蚁. 他的蚂蚁以苹果树上苹果为食. 每个蚁群都需要自己的苹果树来养活自己. 比尔有一张坐标为 n 个蚁群和 n 棵苹果树的地图. 他知道蚂 ...

  6. 【POJ3565】ANTS KM算法

    [POJ3565]ANTS 题意:平面上有2*n个点,N白N黑.为每个白点找一个黑点与之连边,最后所有边不交叉.求一种方案. 题解:KM算法真是一个神奇的算法,虽然感觉KM能做的题用费用流都能做~ 本 ...

  7. 使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题

    一.前言 最近一段时间,网站经常出现两个问题: 1.内存占用率一点点增高,直到将服务器内存占满. 2.访问某个页面时,页面响应过慢,CPU居高不下. 初步判断内存一点点增多可能是因为有未释放的资源一直 ...

  8. poj1852 Ants ——想法题、水题

    求最短时间和最长时间. 当两个蚂蚁相遇的时候,可以看做两个蚂蚁穿过,对结果没有影响.O(N)的复杂度 c++版: #include <cstdio> #define min(a, b) ( ...

  9. Uva10881 Piotr's Ants

    蚂蚁相撞会各自回头.←可以等效成对穿而过,这样移动距离就很好算了. 末状态蚂蚁的顺序和初状态其实是相同的. 那么剩下的就是记录每只蚂蚁的标号,模拟即可. /*by SilverN*/ #include ...

随机推荐

  1. struts配置json需要的jar包

  2. C# winform页面可视化设计打开失败,提示未能加载程序集或他的一个依赖项,dll错误

    这种情况发生在最初项目是x86属性,改成x64后,一些原来dll,页面没有及时更新,导致页面找不到dll, 最简单的解决方式,把项目属性改成AnyCpu,重新编译下,就可以打开可视化设计窗口了.

  3. ANSI C与C89、C99、C11区别差异

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  4. 安装新的int 9中断例程2

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  5. [转]CentOS Apache 性能调试!

    查看Apache的并发请求数及其TCP连接状态: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 返回结果示例 ...

  6. linux_nginx_rewrite

    什么是Nginx的rewrite? 实现URL地址重写,比较复杂的write需要开发来完成,伪静态处理实现是开发的工作, 这rewrite写在location中 指令语法:    rewrite re ...

  7. python_分支循环

    什么是分支+循环? --不同条件进行不同逻辑处理            -- 分支 --满足条件进行反复相同逻辑处理     -- 循环 分支的形式? -- if 条件:  执行体   else: 执 ...

  8. Linux下安装mysql(yum和源码编译两种方式)

    这里介绍Linux下两种安装mysql的方式:yum安装和源码编译安装. 1. yum安装 (1)首先查看centos自带的mysql是否被安装: # yum list installed |grep ...

  9. 02_HTML5+CSS详解第四天

    依旧是CSS部分贴个CSS主要知识点总结的链接:http://blog.csdn.net/html5_/article/details/26098273 [自己的笔记做得好乱,以前一直以为是字丑的原因 ...

  10. Struts2是什么?

    Struts2是什么: Struts2是整合了struts1和webwork的技术优点的使用广泛的MVC框架: Struts2的特点: 1.基于MVC框架,结构清晰,便于开发人员掌控开发流程: 2.使 ...