题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交。
析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可以的,为什么呢,假设有两条线段a1-b1和a2-b2,那么如果相交,dist(a1, b1) + dist(a2, b2) > dist(a1, b2) + dist(a2, b1),自己画图看看就好,然后如果是最小的距离,那么就是可以的了,只要在用使得KM匹配时,把权值取反就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 100 + 50; const LL mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } double w[maxn][maxn], x[maxn], y[maxn]; int prex[maxn], prey[maxn], son[maxn]; double slack[maxn]; int par[maxn]; void adjust(int v){ son[v] = prey[v]; if(prex[son[v]] != -2) adjust(prex[son[v]]); } bool Find(int v){ FOR(i, 0, n) if(prey[i] == -1){ if(slack[i] > x[v] + y[i] - w[v][i]){ slack[i] = x[v] + y[i] - w[v][i]; par[i] = v; } if(x[v] + y[i] == w[v][i]){ prey[i] = v; if(son[i] == -1){ adjust(i); return true; } if(prex[son[i]] != -1) continue; prex[son[i]] = i; if(Find(son[i])) return true; } } return false; } int KM(){ ms(son, -1); ms(y, 0); for(int i = 0; i < n; ++i){ x[i] = 0; for(int j = 0; j < n; ++j) x[i] = max(x[i], w[i][j]); } bool ok; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ prex[j] = prey[j] = -1; slack[j] = inf; } prex[i] = -2; if(Find(i)) continue; ok = false; while(!ok){ double m = inf; FOR(j, 0, n) if(prey[j] == -1) m = min(m, slack[j]); for(int j = 0; j < n; ++j){ if(prex[j] != -1) x[j] -= m; if(prey[j] != -1) y[j] += m; else slack[j] -= m; } FOR(j, 0, n) if(prey[j] == -1 && slack[j] == 0.0){ prey[j] = par[j]; if(son[j] == -1){ adjust(j); ok = true; break; } prex[son[j]] = j; if(Find(son[j])){ ok = true; break; } } } } } double a[maxn], b[maxn]; int main(){ while(scanf("%d", &n) == 1){ for(int i = 0; i < n; ++i) scanf("%lf %lf", a + i, b + i); for(int i = 0; i < n; ++i){ double x, y; scanf("%lf %lf", &x, &y); for(int j = 0; j < n; ++j) w[i][j] = -sqrt(sqr(x-a[j]) + sqr(y-b[j])); } KM(); for(int i = 0; i < n; ++i) printf("%d\n", son[i] + 1); } return 0; }
原文:http://www.cnblogs.com/dwtfukgv/p/7594352.html