[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. (实例篇)LNMP 1.4一键安装包,安装教程

    http://mp.weixin.qq.com/s/l6ijKBwD6tt8jkZytWEIsw https://lnmp.org/download.html 2017-09-11 学习与分享 PHP ...

  2. 百万级别QPS轻量级PHP框架Steeze介绍

    系统简介   Steeze是一个优雅.简洁而又高效的PHP开源框架,在整合了知名框架ThinkPHP和Laravel优点的同时,重写了底层架构,增强了功能实现. 支持swoole模型运行,支持容器.模 ...

  3. fhs文件系统层级结构

    文件系统:操作系统用于明确存储设备或分区上的文件的方法和数据结构:层次化管理文件的结构就是文件系统: linux层次化文件结构,倒树状结构文件结构        FHS  filesystem hie ...

  4. Django_xamin注册model错误

    可能出现的错误: 1. xadmin.sites.AlreadyRegistered: The model UserProfile is already registered 2. error:Fie ...

  5. asp.net web api 向客户端返回错误信息

    1使用Http状态码 ASP.NET Web Api框架提供了Http状态码的值,如下图所示. 虽然有这些预定义的状态码,但在实际项目中使用自定状态码结合预定义状态码更有优势. 通过在适当的位置抛出异 ...

  6. Linux make nginx 的时候报错

    报错如下: `conf/koi-win' and `/usr/local/nginx/conf/koi-win' are the same file   原因: 可能在编译 nginx 的时候步骤不对 ...

  7. tomcat部署最佳实践(一)

    Tomcat部署最佳实践 标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 tomcat是玩web软件必会技能之一,今天我给大家介绍一下tomc ...

  8. CentsOS7无网情况下安装mysql5.7

    1.需求就不用讲了,客户现场,政府环境,银行环境,大多是没网的,所以无网安装是很有必要的 mysql下载路径:https://dev.mysql.com/downloads/mysql/ 查看自己Li ...

  9. 网页窗口logo图标设置

    网站上的logo实际上是一个“**.ico”图片,比如说favicon.ico.实现步骤:第一步:制作favicon.ico,大小一般为16*16毫米(ico图片制作网址http://www.ico. ...

  10. sizeof和strlen的使用

    sizeof和strlen的使用 1. sizeof 其值在编译时就计算好了,所以不能用来返回动态分配的内存空姐的大小. 当参数为下面内容是,所表达的含义: 数组——编译时分配的数组空间大小: 指针— ...