标签:des style http color get width
KM 构图求最小权值匹配
保证最小的权值,所连的边一定是可以不相交的.
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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
Input has several dataset. The first line of each dataset contains a single integer number n(1n100) -- 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(- 10000x, y10000) 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
For each dataset, 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 i -th ant colony. Print a blank line between datasets.
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
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn=220; const double INF=999999999999999.; const double eps=1e-5; struct Dian { double x,y; }white[maxn*maxn],black[maxn*maxn]; int n; double g[maxn][maxn]; int linker[maxn*maxn]; double lx[maxn*maxn],ly[maxn*maxn]; double slack[maxn*maxn]; bool visx[maxn*maxn],visy[maxn*maxn]; bool dfs(int x) { visx[x]=true; for(int y=0;y<n;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; } int KM() { memset(linker,-1,sizeof(linker)); memset(ly,0,sizeof(ly)); for(int i=0;i<n;i++) { lx[i]=-INF; for(int j=0;j<n;j++) if(g[i][j]>lx[i]) lx[i]=g[i][j]; } for(int x=0;x<n;x++) { for(int i=0;i<n;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<n;i++) if(!visy[i]&&d>slack[i]) d=slack[i]; for(int i=0;i<n;i++) if(visx[i]) lx[i]-=d; for(int i=0;i<n;i++) if(visy[i]) ly[i]+=d; else slack[i]-=d; } } } double Dist(Dian a,Dian b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main() { bool first=false; while(scanf("%d",&n)!=EOF) { if(first) putchar(10); else first=true; for(int i=0;i<n;i++) { double a,b; scanf("%lf%lf",&a,&b); white[i]=(Dian){a,b}; } for(int i=0;i<n;i++) { double a,b; scanf("%lf%lf",&a,&b); black[i]=(Dian){a,b}; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { double t=Dist(black[i],white[j]); g[i][j]=-t; } } KM(); for(int i=0;i<n;i++) { printf("%d\n",linker[i]+1); } } return 0; }
UVALive 4043 Ants,布布扣,bubuko.com
标签:des style http color get width
原文地址:http://blog.csdn.net/ck_boss/article/details/35253035