LA4043 - Ants(二分图完备最佳匹配KM)
大致题意:
平面上有n个白点和n个黑点,求一种完美匹配使他们间的连线不相交
思路:要注意到,若有两种匹配相交,总能够当成对角线补成四边形,然后选四边形的两个边作为匹配就不会相交,并且一定匹配后的距离和缩短了,简单的几何知识,显然最小权匹配不会出现相交情况
防止被卡精度用了龙龙
// Accepted C++11 0.079
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i<int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<ll,ll> pii; template <class T>
inline bool RD(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void PT(T x) {
if (x < 0) {
putchar('-'); x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
} const int N = 100+10;
const ll inf = 1LL<<50;
pii poix[N],poiy[N];
int n;
ll mp[N][N]; ll dis(int x,int y){
ll a = poix[x].X-poiy[y].X, b = poix[x].Y-poiy[y].Y;
return (a*a+b*b);
} int link[N];
ll lx[N],ly[N]; //y中各点匹配状态,x,y中的点标号
ll sla[N];
bool visx[N],visy[N];
bool DFS(int x)
{
visx[x] = true;
REP(y,n){
if(visy[y])continue;
ll tmp = lx[x] + ly[y] - mp[x][y];
if(tmp == 0){
visy[y] = true;
if(link[y] == -1 || DFS(link[y])){
link[y] = x;
return true;
}
}
else if(sla[y] > tmp) sla[y] = tmp;
}
return false;
}
ll KM()
{
memset(link,-1,sizeof(link));
memset(ly,0,sizeof(ly));
REP(i,n){
lx[i] = -inf;
REP(j,n) lx[i] = max(lx[i],mp[i][j]);
}
REP(x,n){
REP(i,n) sla[i] = inf;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x)) break;
ll d = inf;
REP(i,n) if(!visy[i]) d = min(d,sla[i]);
REP(i,n){
if(visx[i]) lx[i] -= d;
if(visy[i])ly[i] += d;
else sla[i] -= d;
}
}
}
ll res = 0;
REP(i,n) if(link[i] != -1) res += mp[link[i]][i];
return res;
} int main(){
bool flag = 0;
while(~scanf("%d",&n)){
if(flag) puts("");
flag = 1;
REP(i,n) RD(poix[i].X),RD(poix[i].Y);
REP(i,n) RD(poiy[i].X),RD(poiy[i].Y);
REP(x,n) REP(y,n) mp[y][x] = -sqrt(dis(x,y))*10000000;
KM();
REP(i,n) printf("%d\n",link[i]);
}
//Print a blank line between datasets.
}
LA4043 - Ants(二分图完备最佳匹配KM)的更多相关文章
- Uvalive 4043 Ants —— 二分图最大权匹配 KM算法
题目链接:https://vjudge.net/problem/UVALive-4043 题意: 给出n个白点和n个黑点的坐标, 要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和黑 ...
- KM(Kuhn-Munkres)算法求带权二分图的最佳匹配
KM(Kuhn-Munkres)算法求带权二分图的最佳匹配 相关概念 这个算法个人觉得一开始时有点难以理解它的一些概念,特别是新定义出来的,因为不知道是干嘛用的.但是,在了解了算法的执行过程和原理后, ...
- 二分图匹配之最佳匹配——KM算法
今天也大致学了下KM算法,用于求二分图匹配的最佳匹配. 何为最佳?我们能用匈牙利算法对二分图进行最大匹配,但匹配的方式不唯一,如果我们假设每条边有权值,那么一定会存在一个最大权值的匹配情况,但对于KM ...
- hdu2255 奔小康赚大钱 二分图最佳匹配--KM算法
传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住 ...
- Hdu2255 奔小康赚大钱(二分图最大权匹配KM算法)
奔小康赚大钱 Problem Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好 ...
- 二分图最大权匹配——KM算法
前言 这东西虽然我早就学过了,但是最近才发现我以前学的是假的,心中感慨万千(雾),故作此篇. 简介 带权二分图:每条边都有权值的二分图 最大权匹配:使所选边权和最大的匹配 KM算法,全称Kuhn-Mu ...
- HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) ...
- ACM学习历程—POJ3565 Ants(最佳匹配KM算法)
Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees ...
- 二分图最佳匹配KM算法 /// 牛客暑期第五场E
题目大意: 给定n,有n间宿舍 每间4人 接下来n行 是第一年学校规定的宿舍安排 接下来n行 是第二年学生的宿舍安排意愿 求满足学生意愿的最少交换次数 input 2 1 2 3 4 5 6 7 8 ...
随机推荐
- java 协程框架quasar gradle配置
https://github.com/puniverse/quasar-gradle-template/blob/master/gradle/agent.gradle 1.将其中的"-jav ...
- java 枚举类型的使用
应用 http://blog.csdn.net/qq_27093465/article/details/52180865 原理 http://blog.csdn.net/javazejian/art ...
- move_uploaded_file failed to open stream permission denied
Make sure that: IWPG_user, where user is a system user of the subscription who has rights to "R ...
- 更新yum源导致yum不可用
当安装和yum配置相关的包后报yum模块找不到 yum install -y yum-utils device-mapper-persistent-data lvm2 yum list|grep yu ...
- BZOJ 4161 Shlw loves matrixI ——特征多项式
矩阵乘法递推的新姿势. 叉姐论文里有讲到 利用特征多项式进行递推,然后可以做到k^2logn #include <cstdio> #include <cstring> #inc ...
- SPOJ QTREE4 Query on a tree IV ——动态点分治
[题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...
- BZOJ3130 [Sdoi2013]费用流 【网络流 + 二分】
题目 Alice和Bob在图论课程上学习了最大流和最小费用最大流的相关知识. 最大流问题:给定一张有向图表示运输网络,一个源点S和一个汇点T,每条边都有最大流量.一个合法的网络流方案必须满足:(1)每 ...
- 【bzoj2733】[HNOI2012]永无乡 线段树合并
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- 洛谷 [P3834] 可持久化线段树(主席树)
主席树可以存储线段树的历史状态,空间消耗很大,一般开45n即可 #include <iostream> #include <cstdio> #include <cstri ...
- POJ 2002 Squares [hash]
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 16631 Accepted: 6328 Descript ...