KM   构图求最小权值匹配

保证最小的权值,所连的边一定是能够不相交的.

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]  
[Go Back]   [Status]

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(1n100) --
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(- 10000xy10000) 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

Northeastern Europe 2007-2008

[Submit]  
[Go Back]   [Status]

#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的更多相关文章

  1. UVALive 4043 Ants 蚂蚁(二分图最佳完美匹配,KM算法)

    题意: 有n个蚂蚁n棵树,蚂蚁与树要配对,在配对成功的一对之间连一条线段,要求所有线段不能相交.按顺序输出蚂蚁所匹配的树. 思路: 这个题目真是技巧啊,不能用贪心来为每个蚂蚁选择最近的树,这样很可能是 ...

  2. UVALive 4043 Ants(二分图完美匹配)

    题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ...

  3. UVaLive 4043 Ants (最佳完美匹配)

    题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交. 析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可 ...

  4. Uvalive 4043 Ants —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/UVALive-4043 题意: 给出n个白点和n个黑点的坐标, 要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和黑 ...

  5. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  6. LA - 4043 - Ants

    题意:n只蚂蚁,n棵树,每只蚂蚁要连一棵树,连线(直线)不能相交,给出n只蚂蚁和n棵树的坐标,输出n只蚂蚁所配对的树的编号(1 <= n <= 100, -10000 <= 坐标x, ...

  7. UVALive 4043 转化最佳完美匹配

    首先黑点和白点是组成一个二分图这毫无疑问 关键是题目中要求的所有黑白配的线不能交叉...一开始我也没想到这个怎么转化为二分图里面的算法. 后来看书才知道,如果两两交叉,则可以把两根线当四边形的对角线, ...

  8. UVALive 7505 Hungry Game of Ants (2015Ecfinal)

    题意: 长度是n的线段上点的编号从1~n,每个点有一只蚂蚁蚂蚁的体重等于该点的编号,最初每只蚂蚁可以选择向右走或者向左走两只蚂蚁相遇时体重大的吃掉体重小的并且体重增加为两只的体重和,走到边界时掉头,问 ...

  9. UVa 12709 && UVaLive 6650 Falling Ants (水题)

    题意:给定 n 个长方体的长,宽,高,让你求高最大的时候体积最大是多少. 析:排序,用高和体积排序就好. 代码如下: #pragma comment(linker, "/STACK:1024 ...

随机推荐

  1. qsort函数的简单实践

    #include<stdio.h>#include<stdlib.h>#include<time.h>//利用qsort函数对10个随机数进行排序int compa ...

  2. Web文件管理:elFinder.Net(支持FTP)

    elFinder 是一个基于 Web 的文件管理器,灵感来自 Mac OS X 的 Finder 程序. elFinder.Net是.Net版本的一个Demo,使用ASP.NET MVC 4集成,可以 ...

  3. 函数malloc的实现源代码

    /****************************************************************Copyright 1990, 1994, 2000 by AT&am ...

  4. Swift中的便利构造器和构造器链

    import UIKit // 1.一个类中至少有一个指定构造器, 其必须负责初始化类中所有的实例存储属性 // 2.便利构造器属于次要的, 辅助性的构造器 // 3.类中可以不定义便利构造器, 便利 ...

  5. c++中vector等容器的实现机制

    stl容器区别: vector list deque set map-底层实现 stl容器区别: vector list deque set map (转) 在STL中基本容器有: vector.li ...

  6. 15--Box2D使用(一、创建物理世界)

    创建工程Box2DTest,去掉背景和精灵对象等.首先在HelloWorldScene.h头文件定义一个屏幕像素与物理世界长度转换宏,并引入box2D头文件 #define PIXEL_TO_METE ...

  7. 让IE支持CSS3 Media Query实现响应式Web设计

    如今的屏幕分辨率,小至320px(iPhone),大到2560px甚至更高(大显示器),变化范围极大.除了使用传统的台式机,用户会越来越多的通过手机.上网本.iPad一类的平板设备来浏览页面.这种情况 ...

  8. 当开始输入文字以及完成文字输入时,变换text field的背景以及系统自带一键删除的 叉叉

    当开始输入文字以及完成文字输入时,变换text field的背景. -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{ [tex ...

  9. IOS开发教程之put上传文件的服务器的配置及实例分享-备用

    感谢大神分享 1,HTTP常见的方法 GET 获取指定资源 POST 2M 向指定资源提交数据进行处理请求,在RESTful风格中用于新增资源 HEAD 获取指定资源头部信息PUT 替换指定资源(不支 ...

  10. 修改UITextField Placeholder的颜色

    修改UITextField Placeholder的颜色 1 第一种情况只是修改颜色等文字属性 创建属性字典 NSDictionary *attrsDic = @{ NSForegroundColor ...