【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 ...
随机推荐
- javascript实现可编辑的下拉框
曾经遇到过一个需求的情况是这样的,我们提供给用户的输入框的可选择项只能满足用户的大部分情况的选择,但是有时候会遇到一些用户想要输入的数据是下拉项中所没有的,而用户不希望改变下拉项为输入框模式,需要说如 ...
- ios Object Encoding and Decoding with NSSecureCoding Protocol
Object Encoding and Decoding with NSSecureCoding Protocol February 27, 2014 MISC NSCoding is a fanta ...
- SqlServer Alter Table 语句的用法
更改 字段的数据类型 Alter Table TB_ITM_ITEM alter column is_timing int NULL; 新增字段: alter table WeiboSmartCate ...
- GIS科研站
http://www.3sbase.com/3sbase/ 近年来,地理信息科学发展迅猛,科研人员日益增多,但目前尚缺乏良好的交流平台,致使优秀的科研成果难以推广.同时,对于大量的GIS学生而言,对国 ...
- (hdu)1950 Bridging signals(最长上升子序列)
Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip f ...
- Xcode 5.1 更新后插件不能用
打开目录 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins 右击选择"显示包内容" 找到"Inf ...
- STL库之单链表:forward_list
class template forward_list <forward_list> template < class T, class Alloc = allocator<T ...
- c#根据文件大小显示文件复制进度条实例
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 忘记mysql的root密码
如果忘记root密码或其他用户密码,不要急,按下面操作即可.1. 编辑mysql主配置文件 my.cnfvim /etc/my.cnf 在[mysqld]字段下添加参数 skip-grant ...
- std::string stringf(const char* format, ...)
std::string stringf(const char* format, ...){ va_list arg_list; va_start(arg_list, format); // SUSv2 ...