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 ≤ xy ≤ 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.

题目大意:n个点A和n个点B,每一点A到一点B连一条线,要求n条线互不相交,求一种方案。

思路:可以证明,在费用最小的完美匹配下,所有连线没有相交。这里不证。

PS:WA了几遍才发现平方和最小,和不一定最小……

PS2:跑的是ZKW费用流,我是一个懒人没有改邻接矩阵……

代码(1797MS)(ZKW费用流):

 //忘删调试语句了……
#include <cstdio>
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std; const int MAXV = ;
const int MAXE = MAXV * MAXV;
const double INF = 1e100;
const double EPS = 1e-; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} struct ZWK_FLOW {
int head[MAXV];
double dis[MAXV];
int next[MAXE], to[MAXE], cap[MAXE];
double cost[MAXE];
int st, ed, ecnt, n; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c, double w) {
to[ecnt] = v; cap[ecnt] = c; cost[ecnt] = w; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cap[ecnt] = ; cost[ecnt] = -w; next[ecnt] = head[v]; head[v] = ecnt++;
//printf("%d %d %f\n", u, v - 5, w);
} void SPFA() {
for(int i = ; i <= n; ++i) dis[i] = INF;
priority_queue<pair<double, int> > que;
dis[st] = ;
que.push(make_pair(, st));
while(!que.empty()) {
int u = que.top().second; double d = -que.top().first; que.pop();
if(sgn(d - dis[u]) != ) continue;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(cap[p] && sgn(dis[v] - d - cost[p]) > ) {
dis[v] = d + cost[p];
que.push(make_pair(-dis[v], v));
}
}
}
for(int i = ; i <= n; ++i) dis[i] = dis[ed] - dis[i];
} double minCost;
int maxFlow;
bool use[MAXV]; int add_flow(int u, int aug) {
if(u == ed) {
maxFlow += aug;
minCost += dis[st] * aug;
return aug;
}
use[u] = true;
int now = aug;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(cap[p] && !use[v] && sgn(dis[u] - dis[v] - cost[p]) == ) {
int t = add_flow(v, min(now, cap[p]));
cap[p] -= t;
cap[p ^ ] += t;
now -= t;
if(!now) break;
}
}
return aug - now;
} bool modify_label() {
double d = INF;
for(int u = ; u <= n; ++u) if(use[u])
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(cap[p] && !use[v]) d = min(d, dis[v] + cost[p] - dis[u]);
}
if(sgn(INF - d) == ) return false;
for(int i = ; i <= n; ++i) if(use[i]) dis[i] += d;
return true;
} double min_cost_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
minCost = maxFlow = ;
SPFA();
while(true) {
while(true) {
for(int i = ; i <= n; ++i) use[i] = ;
if(!add_flow(st, 0x7fffffff)) break;
}
if(!modify_label()) break;
}
return minCost;
} void output(int n) {
for(int i = ; i <= n; ++i) {
for(int p = head[i]; p; p = next[p]) {
int &v = to[p];
if(!cap[p]) {printf("%d\n", v - n); break;}
}
}
}
} G; struct Point {
int x, y;
Point() {}
Point(int x, int y): x(x), y(y) {}
void read() {
scanf("%d%d", &x, &y);
}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
double operator * (const Point &rhs) const {
Point a(*this - rhs);
return sqrt(a.x * a.x + a.y * a.y);
}
}; Point p[MAXV];
int n; int main() {
bool flag = true;
while(scanf("%d", &n) != EOF) {
if(flag) flag = false; else puts("");
for(int i = ; i <= * n; ++i) p[i].read();
G.init();
for(int i = ; i <= n; ++i) {
for(int j = n + ; j <= * n; ++j) G.add_edge(i, j, , p[i] * p[j]);
}
int ss = * n + , tt = * n + ;
for(int i = ; i <= n; ++i) G.add_edge(ss, i, , );
for(int i = n + ; i <= n + n; ++i) G.add_edge(i, tt, , );
G.min_cost_flow(ss, tt, tt);
//printf("%d\n", x);
G.output(n);
}
}

代码(94MS)(顺便用一下KM算法):

 //囧,KM都写了顺便改一下吧
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int MAXN = ;
const double INF = 1e100;
const double EPS = 1e-; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} int n;
double mat[MAXN][MAXN], slack[MAXN], Lx[MAXN], Ly[MAXN];
int left[MAXN];
bool S[MAXN], T[MAXN]; bool dfs(int i) {
S[i] = true;
for(int j = ; j <= n; ++j) if(!T[j]) {
double t = Lx[i] + Ly[j] - mat[i][j];
if(sgn(t) == ){
T[j] = true;
if(!left[j] || dfs(left[j])){
left[j] = i;
return true;
}
}
else slack[j] = min(slack[j],t);
}
return false;
} void update() {
double a = INF;
for(int i = ; i <= n; ++i) if(!T[i])
a = min(slack[i],a);
for(int i = ; i <= n; ++i){
if(S[i]) Lx[i] -= a;
if(T[i]) Ly[i] += a; else slack[i] -= a;
}
} void KM() {
for(int i = ; i <= n; ++i) {
Lx[i] = Ly[i] = left[i] = ;
for(int j = ; j <= n; ++j) Lx[i] = max(Lx[i], mat[i][j]);
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) slack[j] = INF;
while(true){
for(int j = ; j <= n; ++j) S[j] = T[j] = ;
if(dfs(i)) break; else update();
}
}
//int ans = 0;
//for(int i = 1; i <=n; ++i) ans += Lx[i] + Ly[i];
//return ans;
} void output() {
for(int i = ; i <= n; ++i) printf("%d\n", left[i]);
} struct Point {
int x, y;
Point() {}
Point(int x, int y): x(x), y(y) {}
void read() {
scanf("%d%d", &x, &y);
}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
double operator * (const Point &rhs) const {
Point a(*this - rhs);
return sqrt(a.x * a.x + a.y * a.y);
}
}; Point p1[MAXN], p2[MAXN]; int main() {
bool flag = true;
while(scanf("%d", &n) != EOF) {
if(flag) flag = false; else puts("");
for(int i = ; i <= n; ++i) p1[i].read();
for(int i = ; i <= n; ++i) p2[i].read();
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) mat[j][i] = -(p1[i] * p2[j]);
}
KM();
output();
}
}

POJ 3565 Ants(最佳完美匹配)的更多相关文章

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

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

  2. POJ 3565 Ants (最小权匹配)

    题意 给出一些蚂蚁的点,给出一些树的点,两两对应,使他们的连线不相交,输出一种方案. 思路 一开始没想到怎么用最小权匹配--后来发现是因为最小权匹配的方案一定不相交(三角形两边之和大于第三边)--还是 ...

  3. Ants(二分图最佳完美匹配)

    Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6904   Accepted: 2164   Special Ju ...

  4. UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)

    UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  5. UVa 11383 少林决胜(二分图最佳完美匹配)

    https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...

  6. 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】

    题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...

  7. UVA - 1045 The Great Wall Game(二分图最佳完美匹配)

    题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...

  8. Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)

    题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...

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

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

随机推荐

  1. android 多线程 异步消息处理 服务 学习笔记 (六)

    三种多线程编程方法 1 class Mythread extends Thread{ @Override public void run(){} } new Mythread().start() 2 ...

  2. 使用dva框架的总结

    最近的项目是react+dva+atd+webpack的一个后台项目,刚接触dva就感觉很喜欢,很简洁用着很爽. 关于使用redux中的一些问题 1.文件切换问题. redux的项目通常哟啊分为red ...

  3. 6.Spring Cloud初相识-------Zool路由

    前言: 在生产环境中,我们不可能将每个服务的真实信息暴漏出去,因为这样太不安全. 我们会选择使用路由代理真实的服务信息,由它负责转发给真实的服务. 新建一个Zool: 1.添加依赖 <?xml ...

  4. C++中的头文件(.h)和源文件(.cpp)都应该写什么?

    头文件(.h):写定义和声明写类的声明(包括类里面的成员和方法的声明).函数原型.#define常数等,但是一般来说不写具体的实现.注意: 1.在写头文件的时候需要注意,在开头和结尾处必须按照如下样式 ...

  5. jsp页面的传值(list)

    jsp页面与xml文件对应的关系: 例:网页上jsp的url为----purchase_app_btn.do? 对应xml文件下的 <action path="/purchase_ap ...

  6. 3930: [CQOI2015]选数

    Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1958  Solved: 979[Submit][Status][Discuss] Descripti ...

  7. 最大的最大公约数( 51nod-1179)

    妈耶有日期显示啊,我还写什么... 给出N个正整数,找出N个数两两之间最大公约数的最大值. 例如:N = 4,4个数为:9 15 25 16,两两之间最大公约数的最大值是15同25的最大公约数5.   ...

  8. 爬虫——urllib.request库的基本使用

    所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib.request.(在python2.x中为urllib2 ...

  9. 查询表名里含有Bill的表有哪些

    Select Name from Master.dbo.sysobjects where xtype='u' and Name like '%Bill%' order by name

  10. Hadoop(14)-MapReduce框架原理-切片机制

    1.FileInputFormat切片机制 切片机制 比如一个文件夹下有5个小文件,切片时会切5个片,而不是一个片 案例分析 2.FileInputFormat切片大小的参数配置 源码中计算切片大小的 ...