题意

给出一些蚂蚁的点,给出一些树的点,两两对应,使他们的连线不相交,输出一种方案。

思路

一开始没想到怎么用最小权匹配……后来发现是因为最小权匹配的方案一定不相交(三角形两边之和大于第三边)……还是too young too simple……没有融会贯通……

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, m) for (int i = begin; i < begin+m; i ++)
using namespace std;
const int MAXV = 205; //X or Y点集大小
const int oo = 0x3fffffff;

const double eps = 1e-8;
template <class weight_type>
struct MaximalMatchingOfWeightedBipartiteGraph{
weight_type w[MAXV][MAXV]; //权值
int sv, tv; //Perfect Matching, sv should equal to tv
bool S[MAXV], T[MAXV];
weight_type lx[MAXV], ly[MAXV]; //X、Y点集可行顶标
int left[MAXV];
weight_type slack[MAXV];
void init(int v){
sv = tv = v;
MEM(w, 0);
}
void add_uedge(int u, int v, weight_type _w){
w[u][v] = _w;
}
bool cross_path(int u){
S[u] = true;
for (int v = 1; v <= tv; v ++){
if(T[v]) continue;
weight_type t = lx[u] + ly[v] - w[u][v];
if (t < eps){
T[v] = true;
if (left[v] == 0 || cross_path(left[v])){
left[v] = u;
return true;
}
}
else{
slack[v] = min(slack[v], t);
}
}
return false;
}
weight_type solve(){
//Init
MEM(left, 0);
for (int i = 1; i <= sv; i ++){
lx[i] = 0;
ly[i] = 0;
for (int j = 1; j <= tv; j ++)
lx[i] = max(lx[i], w[i][j]);
}
//Main
for (int i = 1; i <= sv; i ++){
for (int j = 1; j <= tv; j ++) slack[j] = oo;
while(1){
MEM(S, false); MEM(T, false);
if (cross_path(i)){
break;
}
else{
weight_type d = oo;
for (int j = 1; j <= tv; j ++)
if (!T[j]) d = min(d, slack[j]);
for (int j = 1; j <= sv; j ++)
if (S[j]) lx[j] -= d;
for (int j = 1; j <= tv; j ++){
if (T[j]) ly[j] += d;
else slack[j] -= d;
//匈牙利树中T集点ly不变,S集点lx减小,更新slack值
}
}
}
}
weight_type res = 0;
for(int i = 1; i <= sv; i ++) res += lx[i];
for(int i = 1; i <= tv; i ++) res += ly[i];
return res;
}
};
MaximalMatchingOfWeightedBipartiteGraph <double> km;

struct Point{
double x, y;
}p[MAXV];
double dist(int i, int j){
return sqrt((p[i].x - p[j].x)*(p[i].x - p[j].x) + (p[i].y - p[j].y)*(p[i].y - p[j].y));
}

int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n;
while(scanf("%d", &n) != EOF){
REP(i, 1, n){
scanf("%lf %lf", &p[i+n].x, &p[i+n].y);
}
REP(i, 1, n){
scanf("%lf %lf", &p[i].x, &p[i].y);
}
km.init(n);
REP(i, 1, n){
REP(j, 1, n){
km.add_uedge(i, j, -dist(i, j+n));
}
}
//printf("%f\n", -km.solve());
km.solve();
REP(i, 1, n){
printf("%d\n", km.left[i]);
}
}
return 0;
}
[/cpp]

POJ 3565 Ants (最小权匹配)的更多相关文章

  1. Poj(3686),最小权匹配,多重匹配,KM

    题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...

  2. POJ 3565 Ants 【最小权值匹配应用】

    传送门:http://poj.org/problem?id=3565 Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: ...

  3. poj 2516(拆点+最小权匹配)

    题目链接:http://poj.org/problem?id=2516 思路:考虑某种货物,由于某个订货商可能接受来自不同地区的货物,而某一地区的货物也可能送给不同的订货商,显然不能直接进行匹配,必须 ...

  4. poj 3686(拆点+最小权匹配)

    题目链接:http://poj.org/problem?id=3686 思路:显然工件为X集,机器为Y集合.由于每个机器一次只能加工一个部件,因此我们可以将一台机器拆成N个点,至于部件与机器之间连多大 ...

  5. poj 2195(KM求最小权匹配)

    题目链接:http://poj.org/problem?id=2195 思路:我们都知道KM使用来求最大权匹配的,但如果要求最小权匹配,只需把图中的权值改为负值,求一次KM,然后权值和取反即可. ht ...

  6. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  7. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  8. POJ 2400 最小权匹配

    吐槽:首先,这道题的输入居然是错的.要将上下两个矩阵的位置换一下才可以出样例,也就是上面那个矩阵是employee对Supervisor的打分,下面那个矩阵才是Supervisor对employee的 ...

  9. HDU(1853),最小权匹配,KM

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...

随机推荐

  1. POJ 2181

    #include <iostream> #include <cstdio> #include <cmath> #define MAXN 150005 #includ ...

  2. POJ 1450

    #include <iostream> using namespace std; int main() { //freopen("acm.acm","r&qu ...

  3. Android中的Adapter总结

    一.Adapter的介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for t ...

  4. 深入浅出ES6(十七):展望未来

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 出于对文章长度的考虑,我们还保留了一些尚未提及的新特性,在最后的这篇文章中我会集 ...

  5. Android——横屏和竖屏的切换,以及明文密码的显示

    查看API文档: android.content.pm.ActivityInfo    在手机的使用中,我们要根据不同的需求来改变屏幕的显示方向,一般在浏览信息时是竖屏,在玩游戏的时候就要切换到横屏. ...

  6. UVA 11481 - Arrange the Numbers 数学

    Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You ca ...

  7. JMeter使用指南--转

    JMeter使用指南 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成 ...

  8. JavaWeb项目开发案例精粹-第4章博客网站系统-001设计

    1. 2. 3. # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL_MO ...

  9. TCP建立连接的三次握手过程

    TCP是因特网中的传输层协议,使用三次握手协议建立连接,下面是TCP建立连接的全过程. 上图画出了TCP建立连接的过程.假定主机A运行的是TCP客户程序,B运行的是TCP服务器程序.最初两端的TCP进 ...

  10. html 用图片代替重置按钮

    提交时,若把按钮设置成图片提交特别方便只要 <input type="image" alt="点此提交" src="images/button. ...