给n个点, 定义两点之间距离为|x1-x2|+|y1-y2|。 然后要选出k个城市建机场, 每个机场可以覆盖一个半径的距离。 求在选出点数不大于k的情况下, 这个半径距离的最大值。
二分半径, 然后距离小于等于半径的就连边, 然后跑重复覆盖。
#include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(a)) #define rson m+1, r, rt<<1|1 #define mem1(a) memset(a, -1, sizeof(a)) #define mem2(a) memset(a, 0x3f, sizeof(a)) #define rep(i, a, n) for(int i = a; i<n; i++) #define ull unsigned long long typedef pair<int, int> pll; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; const int maxn = 70; const int maxNode = 5000; int num; struct DLX { int L[maxNode], R[maxNode], U[maxNode], D[maxNode], row[maxNode], col[maxNode]; int S[maxn], H[maxn], sz, n, m, k, x[maxn], y[maxn]; ll dis[maxn][maxn]; void remove(int c) { for(int i = D[c]; i!=c; i = D[i]) { L[R[i]] = L[i]; R[L[i]] = R[i]; } } void resume(int c) { for(int i = U[c]; i!=c; i = U[i]) { L[R[i]] = i; R[L[i]] = i; } } int h() { int cnt = 0; int vis[70]; mem(vis); for(int i = R[0]; i!=0; i = R[i]) { if(!vis[i]) { cnt++; vis[i] = 1; for(int j = D[i]; j!=i; j = D[j]) { for(int k = R[j]; k!=j; k = R[k]) { vis[col[k]] = 1; } } } } return cnt; } int dfs(int d) { if(d+h()>k) return 0; if(R[0] == 0) { return 1; } int c = R[0]; for(int i = R[0]; i!=0; i = R[i]) if(S[c]>S[i]) c = i; for(int i = D[c]; i!=c; i = D[i]) { remove(i); for(int j = R[i]; j!=i; j = R[j]) remove(j); if(dfs(d+1)) return 1; for(int j = L[i]; j!=i; j = L[j]) resume(j); resume(i); } return 0; } void add(int r, int c) { sz++; row[sz] = r; col[sz] = c; S[c]++; U[sz] = U[c]; D[sz] = c; D[U[c]] = sz; U[c] = sz; if(~H[r]) { R[sz] = H[r]; L[sz] = L[H[r]]; L[R[sz]] = sz; R[L[sz]] = sz; } else { H[r] = L[sz] = R[sz] = sz; } } void init() { mem1(H); for(int i = 0; i<=n; i++) { R[i] = i+1; L[i] = i-1; U[i] = i; D[i] = i; } mem(S); R[n] = 0; L[0] = n; sz = n; } int check(ll x) { init(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(dis[i][j] <= x) { add(i, j); } } } if(dfs(0)) return 1; return 0; } void solve() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) { scanf("%d%d", &x[i], &y[i]); } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { dis[i][j] = 1LL*abs(x[i]-x[j])+abs(y[i]-y[j]); } } ll l = 0, r = 4e9+7, ans; for(int i = 0; i < 50; i++) { ll mid = l+r>>1LL; if(check(mid)) { ans = mid; r = mid-1; } else { l = mid+1; } } printf("%lld ", ans); } }dlx; int main() { int t; cin>>t; for(int i = 1; i<=t; i++) { printf("Case #%d: ", i); dlx.solve(); } return 0; }