[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;
}
[poj3565]Ants的更多相关文章
- poj3565 Ants km算法求最小权完美匹配,浮点权值
/** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...
- POJ3565 Ants 和 POJ2195 Going Home
Ants Language:Default Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7975 Accepted: ...
- ACM学习历程—POJ3565 Ants(最佳匹配KM算法)
Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees ...
- POJ3565 Ants (不相交线)
那请告诉我 A - D B - C 和 A - C B - D 那个的和小 显然是A - C B - D (可以根据四边形 对角线大于对边之和) 然后 求的答案是不是就一定是不相交的 就是 ...
- [poj3565] Ants (二分图带权匹配)
传送门 Description 年轻自然主义者比尔在学校研究蚂蚁. 他的蚂蚁以苹果树上苹果为食. 每个蚁群都需要自己的苹果树来养活自己. 比尔有一张坐标为 n 个蚁群和 n 棵苹果树的地图. 他知道蚂 ...
- 【POJ3565】ANTS KM算法
[POJ3565]ANTS 题意:平面上有2*n个点,N白N黑.为每个白点找一个黑点与之连边,最后所有边不交叉.求一种方案. 题解:KM算法真是一个神奇的算法,虽然感觉KM能做的题用费用流都能做~ 本 ...
- 使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题
一.前言 最近一段时间,网站经常出现两个问题: 1.内存占用率一点点增高,直到将服务器内存占满. 2.访问某个页面时,页面响应过慢,CPU居高不下. 初步判断内存一点点增多可能是因为有未释放的资源一直 ...
- poj1852 Ants ——想法题、水题
求最短时间和最长时间. 当两个蚂蚁相遇的时候,可以看做两个蚂蚁穿过,对结果没有影响.O(N)的复杂度 c++版: #include <cstdio> #define min(a, b) ( ...
- Uva10881 Piotr's Ants
蚂蚁相撞会各自回头.←可以等效成对穿而过,这样移动距离就很好算了. 末状态蚂蚁的顺序和初状态其实是相同的. 那么剩下的就是记录每只蚂蚁的标号,模拟即可. /*by SilverN*/ #include ...
随机推荐
- eclipse中如何同期化
打开MyEclipse8.0help->Software Updates->find and install(如果没有这个就用help->Software Updates->A ...
- Centos 6.9安装配置MongoDB
注意:centos6上就不要装mongo3了,容易出错. 1. 下载 curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2 ...
- Redis的事务功能详解
Redis的事务功能详解 MULTI.EXEC.DISCARD和WATCH命令是Redis事务功能的基础.Redis事务允许在一次单独的步骤中执行一组命令,并且可以保证如下两个重要事项: >Re ...
- 2017-06-28(passwd 主要组与附属组 gpasswd newgrp groups)
passwd passwd -l 用户名 (锁定用户) passwd -u 用户名 (解锁用户) passwd -d 用户名 (清楚用户密码) 主要组与附属组 一个用户可以同时属于多个组,其中一个 ...
- js_9_dom属性
如何设置标签属性? 找到标签 设置属性: 默认:.属性 = 属性值 // 默认属性才能用 找style中font-size : .style.fontSize // 中间的短杆去掉,s要 ...
- Netty初探
匠心零度 转载请注明原创出处,谢谢! 说在前面 为什么我们需要学习netty?谈谈自己的看法,由于本人水平有限,如果有那里不对,希望各位大佬积极指出,欢迎在留言区进行评论交流.探讨. 由于移动互联网的 ...
- 控制器没有足够的带宽可利用为USB大容量存储设备的解决方法
伴随网盘时代的没落,最近刚入手了一个移动硬盘.现在的移动硬盘都是USB3.0,传输速度比USB2.0要快很多.但是链接笔记本电脑后发现传输速度在20MB/s左右,跟USB2.0速度差不多,并不能达到传 ...
- js 生成 UUID
在项目中遇到要生成 UUID 的需求,第一反应就是有没有原生的生成方法,找了找,发现没有,只能自己建立算法 function. 下面是我用的算法 function uuid(len, radix) { ...
- 解决IE中placeholder的兼容问题
定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 &l ...
- 【转】Vim使用笔记
http://www.cnblogs.com/jiqingwu/archive/2012/06/14/vim_notes.html 曾经使用了两年多的Vim,手册也翻过一遍.虽然现在不怎么用vim了, ...