poj3565-Ants
来源:互联网 发布:数据库小图标 编辑:程序博客网 时间:2024/06/05 19:57
poj3565-Ants
Ants
Time Limit: 5000MS
Memory Limit: 65536K
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
Northeastern Europe 2007
考虑abcd是平面上任意四个点,无妨设ab为ant,cd为apple,由三角形法则,连线交叉时比不交叉时短。题目中说明存在一个合法的匹配,对所有匹配状态中任意一个状态,若存在四个点两两连线交叉的点对,则修改为两两不相交,总距离变短,此操作使总距离严格单调减。又存在最终状态使得两两连线不相交,此操作有界。故次操作收敛。所以此题要求完全图的最小权匹配。只需将边权取负,跑一遍最大权匹配即可。
最大权匹配
double U[MAXN], V[MAXN], slack[MAXN], w[MAXN][MAXN];int n, R[MAXN], L[MAXN];bool tipU[MAXN], tipV[MAXN];bool find(int u) { tipU[u] = 1; for (int i = 1; i <= n; ++i) { if (tipV[i]) continue; double temp = U[u] + V[i] - w[u][i]; if (temp <= esp) { tipV[i] = 1; if (!R[i] || find(R[i])) { R[i] = u; L[u] = i; return true; } } else if (temp < slack[i]) slack[i] = temp; } return false;}void KM(){ for (int i = 1; i <= n; ++i) { U[i] = -INF, V[i] = R[i] = L[i] = 0; for (int j = 1; j <= n; ++j) if (U[i] < w[i][j]) U[i] = w[i][j]; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) slack[j] = INF; while (true) { memset(tipU, 0, sizeof tipU); memset(tipV, 0, sizeof tipV); if (find(i)) break; double d = INF; for (int j = 1; j <= n; ++j) if (!tipV[j] && d > slack[j]) d = slack[j]; for (int j = 1; j <= n; ++j) if (tipU[j]) U[j] -= d; for (int j = 1; j <= n; ++j) if (tipV[j]) V[j] += d; else slack[j] -= d; } } for (int i = 1; i <= n; ++i) printf("%d\n", L[i]);}
u[],v[]两侧的点标。关于最大权匹配slack松弛值:松弛的是v[]一侧的值。在代码
for (int j = 1; j <= n; ++j) if (tipV[j]) V[j] += d; else slack[j] -= d;
中,slack[j]-=d是因为slack[i]-=d。
此题代码
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAXN = 110;const double INF = 1e11;const double esp = 1e-5;struct Point{ double x, y;}p[MAXN], q[MAXN];double dis(Point a, Point b){ return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double U[MAXN], V[MAXN], slack[MAXN], w[MAXN][MAXN];int n, R[MAXN], L[MAXN];bool tipU[MAXN], tipV[MAXN];bool find(int u) { tipU[u] = 1; for (int i = 1; i <= n; ++i) { if (tipV[i]) continue; double temp = U[u] + V[i] - w[u][i]; if (temp <= esp) { tipV[i] = 1; if (!R[i] || find(R[i])) { R[i] = u; L[u] = i; return true; } } else if (temp < slack[i]) slack[i] = temp; } return false;}void KM(){ for (int i = 1; i <= n; ++i) { U[i] = -INF, V[i] = R[i] = L[i] = 0; for (int j = 1; j <= n; ++j) if (U[i] < w[i][j]) U[i] = w[i][j]; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) slack[j] = INF; while (true) { memset(tipU, 0, sizeof tipU); memset(tipV, 0, sizeof tipV); if (find(i)) break; double d = INF; for (int j = 1; j <= n; ++j) if (!tipV[j] && d > slack[j]) d = slack[j]; for (int j = 1; j <= n; ++j) if (tipU[j]) U[j] -= d; for (int j = 1; j <= n; ++j) if (tipV[j]) V[j] += d; else slack[j] -= d; } } for (int i = 1; i <= n; ++i) printf("%d\n", L[i]);}int main(){ while(scanf("%d", &n) != EOF) { for (int i = 1; i <= n; ++i) scanf("%lf%lf", &p[i].x, &p[i].y); for (int i = 1; i <= n; ++i) scanf("%lf%lf", &q[i].x, &q[i].y); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) w[i][j] = -dis(p[i], q[j]); KM(); } return 0;}
- poj3565 Ants
- POJ3565-Ants
- poj3565 Ants
- poj3565 ants
- poj3565-Ants
- POJ3565 Ants
- poj3565 ants(KM)
- 【KM】POJ3565[Ants]题解
- poj3565 Ants(KM)
- POJ3565 Ants【二分图最佳匹配】
- poj3565 Ants(训练手册例题翻译)
- 弱校联萌十一大决战之厉兵秣马A ants【八皇后拓展】poj3565
- poj3565(KM)
- Ants
- Ants
- Ants
- Ants
- Ants
- 我的第一次div布局
- Ubuntu16.04LTS 安装搜狗输入法
- clang 在Ubuntu上的配置
- Android onClick 按钮单击事件 四种常用写法
- hdu 1847 Good Luck in CET-4 Everybody!(基础博弈)
- poj3565-Ants
- 1.oracle RAC11G 对单机ADG搭建详细文档
- hihocoder week171分析---并查集
- Redis安装和自启动配置
- BZOJ 2201 期望DP 解题报告
- 浅谈Java中Collection、Iterable、List、ArrayList间的关系
- JSP之3大指令、9大内置对象、4大域对象、动作标签
- 定义常量类
- HDU3265(线段树+扫描线+挖空矩形面积并)