Ants (poj 3565 最小权匹配)
来源:互联网 发布:淘宝销售假冒产品判刑 编辑:程序博客网 时间:2024/04/29 06:04
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 (−
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 5844 867 2899 34-13 -59-47 -4486 7468 -75-68 6099 -60
Sample Output
42153
Source
题意:告诉n个蚂蚁和n棵苹果树,问每只蚂蚁去哪个苹果树使得所有路径不相交。
思路:最小权匹配能保证路径不相交。
代码:
#include <iostream>#include <functional>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;const int MAXN = 105;const int MAXM = 200010;const int N = 1005;struct Node{ double x,y;}ant[MAXN],apple[MAXN];int nx,ny;double g[MAXN][MAXN];int linker[MAXN];double lx[MAXN],ly[MAXN];double slack[MAXN];bool visx[MAXN],visy[MAXN];bool DFS(int x){ visx[x]=true; for (int y=0;y<ny;y++) { if (visy[y]) continue; double tmp=lx[x]+ly[y]-g[x][y]; if (fabs(tmp)<eps) { visy[y]=true; if (linker[y]==-1||DFS(linker[y])) { linker[y]=x; return true; } } else if (slack[y]>tmp) slack[y]=tmp; } return false;}void KM(){ memset(linker,-1,sizeof(linker)); memset(ly,0,sizeof(ly)); for (int i=0;i<nx;i++) { lx[i]=-INF; for (int j=0;j<ny;j++) { if (g[i][j]>lx[i]) lx[i]=g[i][j]; } } for (int x=0;x<nx;x++) { for (int i=0;i<ny;i++) slack[i]=INF; while (true) { memset(visx,false,sizeof(visx)); memset(visy,false,sizeof(visy)); if (DFS(x)) break; double d=INF; for (int i=0;i<ny;i++) if (!visy[i]&&d>slack[i]) d=slack[i]; for (int i=0;i<nx;i++) if (visx[i]) lx[i]-=d; for (int i=0;i<ny;i++){ if (visy[i]) ly[i]+=d; else slack[i]-=d; } } } for (int i=0;i<nx;i++) printf("%d\n",linker[i]+1);}double Dis(Node a,Node b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){#ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);#endif int i,j; while (~scanf("%d",&nx)) { ny=nx; for (i=0;i<nx;i++) scanf("%lf%lf",&ant[i].x,&ant[i].y); for (i=0;i<nx;i++) scanf("%lf%lf",&apple[i].x,&apple[i].y); for (i=0;i<nx;i++) for (j=0;j<nx;j++) g[i][j]=-1.0*Dis(apple[i],ant[j]); KM(); } return 0;}
- Ants (poj 3565 最小权匹配)
- poj 3565 Ants 最小权匹配
- POJ 3565 Ants(二分图最小权完备匹配)
- POJ 3565 Ants (最小权完美匹配 KM算法)
- POJ 3565 Ants(匹配? 计算几何!)
- POJ 3565 最小权匹配
- uva 1411 Ants (权值和最小的完美匹配---KM算法)
- 分治法,最小权完美匹配(巨人和鬼,Ants,uva 1411)
- POJ 3565 Ants (KM)
- Ants POJ - 3565 (神题)
- POJ 3686 最小权匹配
- POJ 2400 最小权匹配
- poj 2400 最小权匹配+匹配输出
- POJ 3565 Ants(计算几何,KM)
- poj 3565 Ants(几何调整思想)
- poj 3565 Ants (KM算法)
- POJ 3565 Ants
- poj 3565 Ants KM
- UC/OS II消息队列管理实例
- 三极管的材质、管型以及极性判断
- 剑指offer—对称的二叉树
- String、StringBuffer、StringBuilder
- Leetocde_242_Valid Anagram
- Ants (poj 3565 最小权匹配)
- linux sizeof 详解
- hdu1097 A hard puzzle
- Lintcode 二叉树的后序遍历
- 【SQL优化】四月份培训的一个sql优化案例3
- 关于内存分配
- Mybatis框架基础学习(一)
- Linux常用命令大全
- php 通过txt文件将数据装填入数据库