【UVA 1411】 Ants (KM)
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 the picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles.
One possible connection is denoted by lines.
Input
Input has several dataset. The first line of each dataset 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 (−10000 ≤ x, y ≤ 10000) 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
For each dataset, 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.
Print a blank line between datasets.
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
【题意】
给出平面上n个白点n个黑点,要求两两配对,且配对所连线段没有交点。
【分析】
处理不相交的方法,用点的欧几里德距离作为边权进行KM,那么对于ABCD四个点来说,一定取的是不相交那一种。
因为是有小数点的KM,于是我又打了一下。。。
这种要证明有界性的算法真的打错了一点点就好容易RE,又不会调,(模拟真是太难了,不如肉眼找错= =)
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 110
#define Maxm 10010
#define INF 0xfffffff struct node
{
int x,y,next;
double c;
}t[Maxm];int len;
int first[Maxn]; double mymax(double x,double y) {return x>y?x:y;}
double mymin(double x,double y) {return x<y?x:y;}
double fabs(double x) {return x<?-x:x;} void ins(int x,int y,double c)
{
t[++len].x=x;t[len].y=y;t[len].c=-c;
t[len].next=first[x];first[x]=len;
} int n;
double lx[Maxn],ly[Maxn],slack[Maxn];
int match[Maxn];
bool visx[Maxn],visy[Maxn]; int ax[Maxn],ay[Maxn],bx[Maxn],by[Maxn]; bool ffind(int x)
{
visx[x]=;
for(int i=first[x];i;i=t[i].next) if(!visy[t[i].y])
{
int y=t[i].y;
if(fabs(lx[x]+ly[y]-t[i].c)<0.00001)
{
visy[y]=;
if(!match[y]||ffind(match[y]))
{
match[y]=x;
return ;
}
}
else slack[y]=mymin(slack[y],lx[x]+ly[y]-t[i].c);
}
return ;
} void solve()
{
memset(match,,sizeof(match));
for(int i=;i<=n;i++)
{
ly[i]=;
lx[i]=-INF;
for(int j=first[i];j;j=t[j].next) lx[i]=mymax(lx[i],t[j].c);
}
int i;
for(i=;i<=n;i++)
{
for(int j=;j<=n;j++) slack[j]=INF;
while()
{
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(ffind(i)) break;
double delta=INF;
for(int j=;j<=n;j++) if(!visy[j])
delta=mymin(delta,slack[j]);
if(fabs(delta-INF)<0.00001) return;
for(int j=;j<=n;j++)
{
if(visx[j]) lx[j]-=delta;
if(visy[j]) ly[j]+=delta;
else if(fabs(slack[j]-INF)>0.00001) slack[j]-=delta;
}
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d%d",&bx[i],&by[i]);
for(int i=;i<=n;i++) scanf("%d%d",&ax[i],&ay[i]);
// for(int i=1;i<=n;i++) scanf("%d%d",&bx[i],&by[i]);
len=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
double d=(double)((ax[i]-bx[j])*(ax[i]-bx[j])+(ay[i]-by[j])*(ay[i]-by[j]));
d=sqrt(d);
ins(i,j,d);
}
solve();
for(int i=;i<=n;i++) printf("%d\n",match[i]);
}
return ;
}
[UVA 1411]
2016-10-27 14:50:29
【UVA 1411】 Ants (KM)的更多相关文章
- 【HDU 2853】Assignment (KM)
Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...
- 【UOJ#311】【UNR #2】积劳成疾(动态规划)
[UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...
- 【UOJ#246】套路(动态规划)
[UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...
- 【LOJ#6074】子序列(动态规划)
[LOJ#6074]子序列(动态规划) 题面 LOJ 题解 考虑一个暴力\(dp\). 设\(f[i][c]\)表示当前在第\(i\)位,并且以\(c\)结尾的子序列个数. 那么假设当前位为\(a\) ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 通俗地说逻辑回归【Logistic regression】算法(二)sklearn逻辑回归实战
前情提要: 通俗地说逻辑回归[Logistic regression]算法(一) 逻辑回归模型原理介绍 上一篇主要介绍了逻辑回归中,相对理论化的知识,这次主要是对上篇做一点点补充,以及介绍sklear ...
- 【LOJ#2687】Vim(动态规划)
[LOJ#2687]Vim(动态规划) 题面 LOJ 题解 发现移动的路径一定是每次往后跳到下一个某个字符的位置,然后往回走若干步,删掉路径上的所有\(e\),然后继续执行这个操作. 这里稍微介绍一下 ...
- 【UOJ#76】【UR #6】懒癌(动态规划)
[UOJ#76][UR #6]懒癌(动态规划) 题面 UOJ 题解 神....神仙题. 先考虑如果是完全图怎么做... 因为是完全图,所以是对称的,所以我们只考虑一个有懒癌的人的心路历程. 如果只有一 ...
- 【UOJ#22】【UR #1】外星人(动态规划)
[UOJ#22][UR #1]外星人(动态规划) 题面 UOJ 题解 一道简单题? 不难发现只有按照从大往小排序的顺序选择的才有意义,否则先选择一个小数再去模一个大数是没有意义的. 设\(f[i][j ...
随机推荐
- E: Sub-process /usr/bin/dpkg returned an error code (1)
E: Sub-process /usr/bin/dpkg returned an error code (1) 错误描述 dpkg: error processing archive /var/cac ...
- ThinkPHP函数详解:cache方法
cache方法是3.0版本开始新增的缓存管理方法.注意:3.1.2版本后因cache方法并入原S方法,所以cache方法不再建议使用,用S方法即可. cache 用于缓存设置.获取.删除操作 用法ca ...
- ADO.NET连接数据库的两种方式
//实现了IDisposable接口的类,用using括起来 //插入数据 string connString = "Data Source=(local);Initial Catalog= ...
- 多个线程访问url
多个线程访问url package com.aig.ecompassios.ecard; import java.io.BufferedReader; import java.io.InputStre ...
- HTML解析引擎:Jumony
Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...
- 关于IOS网络通信的学习
最近由于需要在看关于网络通信方面的知识,所以在网上找了找关于网络解释方面的知识.找了半天没有找到一篇能详细解释通讯流程的,心里忍不住就万马奔腾了.没办法,谁让自己想学呢!于是又找了找,觉得没有满意的. ...
- 第17条:实现description方法
自定义类需要自己覆写description方法,否则打印信息时就会调用NSObject类所实现的默认方法.(如果不覆写,只会输出类名和对象的内存地址的信息,这只有在判断两个指针是否指向同一对象时才有用 ...
- linux系统防火墙对访问服务器的影响
一.刚部署好的linux服务器默认开启了防火墙,这时假如你在该服务器装一个tomcat并启动,在别的机器访问该tomcat是不成功的.需要关闭服务器防火墙才可以 二.service iptables ...
- UVA 10795 A Different Task(汉诺塔 递归))
A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...
- ubuntu zend-eclipse-php debugger调试
1 下载zendDebugger http://www.zend.com/en/products/studio/downloads?src=downloadb 在这个页面中找到zend debugge ...