UVALive 4043 Ants
KM 构图求最小权值匹配
保证最小的权值,所连的边一定是能够不相交的.
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description

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
Input has several dataset. The first line of each dataset contains a single integer number n(1n
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(- 10000x, 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 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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; const int maxn=220;
const double INF=999999999999999.;
const double eps=1e-5; struct Dian
{
double x,y;
}white[maxn*maxn],black[maxn*maxn]; int n;
double g[maxn][maxn];
int linker[maxn*maxn];
double lx[maxn*maxn],ly[maxn*maxn];
double slack[maxn*maxn];
bool visx[maxn*maxn],visy[maxn*maxn]; bool dfs(int x)
{
visx[x]=true;
for(int y=0;y<n;y++)
{
if(visy[y]) continue;
double tmp=lx[x]+ly[y]-g[x][y];
if(fabs(tmp)<eps)
{
visy[y]=true;
if(linker[y]==-1||dfs(linker[y]))
{
linker[y]=x;
return true;
}
}
else if(slack[y]>tmp)
slack[y]=tmp;
}
return false;
} int KM()
{
memset(linker,-1,sizeof(linker));
memset(ly,0,sizeof(ly));
for(int i=0;i<n;i++)
{
lx[i]=-INF;
for(int j=0;j<n;j++)
if(g[i][j]>lx[i])
lx[i]=g[i][j];
}
for(int x=0;x<n;x++)
{
for(int i=0;i<n;i++)
slack[i]=INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(dfs(x)) break;
double d=INF;
for(int i=0;i<n;i++)
if(!visy[i]&&d>slack[i])
d=slack[i];
for(int i=0;i<n;i++)
if(visx[i])
lx[i]-=d;
for(int i=0;i<n;i++)
if(visy[i])
ly[i]+=d;
else
slack[i]-=d;
}
}
} double Dist(Dian a,Dian b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} int main()
{
bool first=false;
while(scanf("%d",&n)!=EOF)
{
if(first) putchar(10);
else first=true;
for(int i=0;i<n;i++)
{
double a,b;
scanf("%lf%lf",&a,&b);
white[i]=(Dian){a,b};
}
for(int i=0;i<n;i++)
{
double a,b;
scanf("%lf%lf",&a,&b);
black[i]=(Dian){a,b};
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
double t=Dist(black[i],white[j]);
g[i][j]=-t;
}
}
KM();
for(int i=0;i<n;i++)
{
printf("%d\n",linker[i]+1);
}
}
return 0;
}
UVALive 4043 Ants的更多相关文章
- UVALive 4043 Ants 蚂蚁(二分图最佳完美匹配,KM算法)
题意: 有n个蚂蚁n棵树,蚂蚁与树要配对,在配对成功的一对之间连一条线段,要求所有线段不能相交.按顺序输出蚂蚁所匹配的树. 思路: 这个题目真是技巧啊,不能用贪心来为每个蚂蚁选择最近的树,这样很可能是 ...
- UVALive 4043 Ants(二分图完美匹配)
题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ...
- UVaLive 4043 Ants (最佳完美匹配)
题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交. 析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可 ...
- Uvalive 4043 Ants —— 二分图最大权匹配 KM算法
题目链接:https://vjudge.net/problem/UVALive-4043 题意: 给出n个白点和n个黑点的坐标, 要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和黑 ...
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...
- LA - 4043 - Ants
题意:n只蚂蚁,n棵树,每只蚂蚁要连一棵树,连线(直线)不能相交,给出n只蚂蚁和n棵树的坐标,输出n只蚂蚁所配对的树的编号(1 <= n <= 100, -10000 <= 坐标x, ...
- UVALive 4043 转化最佳完美匹配
首先黑点和白点是组成一个二分图这毫无疑问 关键是题目中要求的所有黑白配的线不能交叉...一开始我也没想到这个怎么转化为二分图里面的算法. 后来看书才知道,如果两两交叉,则可以把两根线当四边形的对角线, ...
- UVALive 7505 Hungry Game of Ants (2015Ecfinal)
题意: 长度是n的线段上点的编号从1~n,每个点有一只蚂蚁蚂蚁的体重等于该点的编号,最初每只蚂蚁可以选择向右走或者向左走两只蚂蚁相遇时体重大的吃掉体重小的并且体重增加为两只的体重和,走到边界时掉头,问 ...
- UVa 12709 && UVaLive 6650 Falling Ants (水题)
题意:给定 n 个长方体的长,宽,高,让你求高最大的时候体积最大是多少. 析:排序,用高和体积排序就好. 代码如下: #pragma comment(linker, "/STACK:1024 ...
随机推荐
- POJ 2752 Seek the Name, Seek the Fame(next数组的理解)
做此题,只要理解好next数组就行....................... #include <cstdio> #include <cmath> #include < ...
- Netmon: A light-weight network monitor for Windows
Netmon is a light-weight network monitor that works on Windows operating systems. It provides differ ...
- Pythn中的super用法
在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: 代码如下: class A: def __init__(self): p ...
- java基础之导出(Excel)
function exportExcel() { $.messager .confirm( '提示信息', '您确定要导出到Excel?', function(r) { if (r) { var co ...
- tomcat程序记录客户端真实IP
需求: 开发告知:让后端tomcat日志获取真实的IP,而不是nginx 服务器的IP tomcat前面是nginx做的反向代理,所以tomcat取到的是nginx的ip. 日志名称是localhos ...
- (转)jQuery验证控件jquery.validate.js使用说明+中文API
官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...
- 通俗理解angularjs中的$apply,$digest,$watch
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...
- TCP/IP 要点备忘
1. 3次握手/4次挥手过程,以及状态变化: 2. RTT,TTL,TOS(8位服务类型,最小延时.最大吞吐.最高可用.最小费用). 3. TimeWait(2msl)状态,防止最后一个ack丢失 4 ...
- GridView Footer页脚统计实现多行
在使用GridView时有时会需要多行显示页脚Footer的统计,下面是一种解决方法,仅仅供各位参考 在GridView的RowCreated事件中添加多行页脚,实例代码如下: protected v ...
- ajax初学
//参数说明url:请求文件地址//fnSucc:请求成功执行的函数,请求成功的条件为readyState状态码为4:且status状态码为200,status状态为浏览器请求返回的状态码//在fnS ...