Ants(二分图最佳完美匹配)
Ants
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 6904 | Accepted: 2164 | Special Judge |
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
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
Source
//# include <bits/stdc++.h>
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <math.h>
using namespace std;
# define eps 1e-
# define LL long long
# define INF 1e20
# define MX
struct Node{
double x,y;
}ant[MX], tree[MX]; int n;
double W[MX][MX];
double Lx[MX], Ly[MX];
int link[MX]; //右边匹配左的
bool S[MX], T[MX]; double dist(int x,int y){
return sqrt((ant[x].x-tree[y].x)*(ant[x].x-tree[y].x)+(ant[x].y-tree[y].y)*(ant[x].y-tree[y].y));
} bool match(int p)
{
S[p]=;
for (int i=;i<=n;i++)
{
if (!T[i]&&(fabs(Lx[p]+Ly[i]-W[p][i])<=eps))
{
T[i]=;
if (link[i]==- || match(link[i]))
{
link[i]=p;
return ;
}
}
}
return ;
} void update()
{
double del = INF;
for (int i=;i<=n;i++) if(S[i])
for (int j=;j<=n;j++) if (!T[j])
if (Lx[i]+Ly[j]-W[i][j] < del - eps)
del = Lx[i]+Ly[j]-W[i][j];
for (int i=;i<=n;i++)
{
if (S[i]) Lx[i]-=del;
if (T[i]) Ly[i]+=del;
}
} void KM()
{
memset(link,-,sizeof(link));
for (int i=;i<=n;i++)
{
Ly[i]=0.0;
Lx[i]=-INF;
for (int j=;j<=n;j++)
if (W[i][j] > Lx[i] + eps)
Lx[i] = W[i][j];
}
for (int i=;i<=n;i++)
{
while ()
{
memset(S,,sizeof(S));
memset(T,,sizeof(T));
if (match(i)) break;
else update();
}
}
} int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<=n;i++)
scanf("%lf%lf",&ant[i].x, &ant[i].y);
for (int i=;i<=n;i++)
scanf("%lf%lf",&tree[i].x, &tree[i].y);
for (int i=;i<=n;i++)
for(int j=;j<=n;j++)
W[i][j] = -dist(i,j);
KM();
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= n; ++j)
{
if(link[j] == i)
{
printf("%d\n",j);
break;
}
}
}
}
return ;
}
Ants(二分图最佳完美匹配)的更多相关文章
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- UVa 11383 少林决胜(二分图最佳完美匹配)
https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...
- UVA - 1045 The Great Wall Game(二分图最佳完美匹配)
题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...
- 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】
题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...
- Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)
题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...
- ZOJ-3933 Team Formation (二分图最佳完美匹配)
题目大意:n个人,分为两个阵营.现在要组成由若干支队伍,每支队伍由两个人组成并且这两个人必须来自不同的阵营.同时,每个人都有m个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ...
- UVa 1349 - Optimal Bus Route Design(二分图最佳完美匹配)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- POJ 3565 Ants(最佳完美匹配)
Description Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on ...
- UVALive 4043 Ants 蚂蚁(二分图最佳完美匹配,KM算法)
题意: 有n个蚂蚁n棵树,蚂蚁与树要配对,在配对成功的一对之间连一条线段,要求所有线段不能相交.按顺序输出蚂蚁所匹配的树. 思路: 这个题目真是技巧啊,不能用贪心来为每个蚂蚁选择最近的树,这样很可能是 ...
随机推荐
- Windows右击无新建文本文档怎么办
右击无新建文本文档2008-07-26 16:51 刚在网上找的,在运行项输入notepad,把下面的复制进去,然后保存为123.reg,双击导入. REGEDIT4 [HKEY_CLASSES_RO ...
- libevent2源码分析之五:关键的调用链
用一个调用链来表示函数调用的流程,看起来更直观.根据上面的分析,总结了一些重要的调用链. 初始化 event_base_new event_base_new_with_config min_heap_ ...
- EntityFramework.SqlServer.dll 中发生 其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接)
解决方案: 1.打开Sql server 管理配置器 或者在命令行输入:SQLServerManager10.msc 2.点击MSSQLSERVER的协议,在右侧的页面中选择TCP/IP协议启用 3. ...
- webpack 环境搭建基础框架
一.安装babel相关 1,安装依赖 cnpm i -D babel-core babel-loader babel-preset-env babel-preset-stage- babel-plug ...
- Javascript 与 SPA单页Web富应用
书单推荐 # <单页Web应用:JavaScript从前端到后端> http://download.csdn.net/detail/epubitbook/8720475 # <MVC ...
- javascript跨浏览器事件对象类库
一.前言 学习了javascript事件后,个人总结归纳了跨浏览器事件对象类库,方便以后使用,现分享给大家. 二.事件对象封装 将对浏览器事件对象的操作封装成eventObject.js方便调用 // ...
- atitit.guice3 绑定方式打总结生成非单例对象toInstance toProvider区别 v2 pb29
atitit.guice3 绑定方式打总结生成非单例对象toInstance toProvider区别 v2 pb29 1. 三 绑定方式的介绍1 2. To接口,链式绑定,用的最多的1 3. toC ...
- Angularjs学习笔记11_手工初始化
http://my.oschina.net/fuckBAT/blog/375579 Angular的编译机制允许开发人员给浏览器添加新的Html语法,允许我们添加一些html节点,attribute, ...
- C# AppDomain 类
/*** AppDomain 表示应用程序域,它是一个应用程序在其中执行的独立环境.无法继承此类. 应用程序域(由 AppDomain 对象表示)为执行托管代码提供隔离.卸载和安全边界. 使用应用程序 ...
- windows system.exe占用文件
1)问题的原因是出于一个服务Application Experience,如果装好系统后就把此服务设为手动启动了,平时运行也没什么异常.但是win7在运行exe时如果没有这个服务的辅助就会长时间的占用 ...