This year's Shanghai online game, a topic, the same as HDU 2295. Is the calculation of the distance is a Euclidean distance, one is the Manhattan distance
After learning DLX feel this good water, is a bare repeated cover
Watch out, don't spill.
#include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> #include <stdlib.h>using namespace std; const int MAXN = 60 + 10; const int MAXR = 60 + 10; const int Maxnode = * + MAXR + 10; #define FOR (L, A, s) for (int i = a[s]; i = s; i = a[i]) struct dlx{//MAXN number of columns, Maxnode total nodes, maxr rows int n, SZ; int S[MAXN]; int Row[maxnode], col[maxnode]; int L[maxnode], R[maxnode], U[maxnode], D[maxnode]; int H[MAXR]; int ANSD, ANS[MAXR]; void init (int n) {n = n; The first line of the virtual node for (int i = 0; I <= n; i + +) {U[i] = d[i] = i; L[i] = i-1; R[i] = i + 1; } R[n] = 0; L[0] = n; SZ = n + 1; The number of each column is memset (s, 0, sizeof (s)); H[i] = 1 means that the line has not yet 1//otherwise represents the first 1 of the SZ is how many memset (H,-1, sizeof (h)); }//Add a 1 void Link (int r, int c) {Row[sz] = R in column C of section R; COL[SZ] = C ; S[C] + +; D[SZ] = c; U[SZ] = U[c]; D[U[C]] = SZ; U[C] = sz; if (H[r] < 0) {H[r] = L[sz] = R[sz] = sz;} else{R[sz] = H[r]; L[SZ] = L[h[r]]; R[L[SZ]] = SZ; L[R[SZ]] = SZ; } sz + +; }//Delete column C void remove (int c) {for (I, D, c) {l[r[i]] = l[i]; R[l[i]] = r[i]; }}//Recover column C void restore (int c) {for (I, D, c) {l[r[i]] = r[l[i]] = i; }} bool VIS[MAXN]; int f () {int ans = 0; For (i, R, 0) vis[i] = true; For (i, R, 0) {if (Vis[i]) {vis[i] = false; Ans + +; For (J, D, I) for (K, R, J) Vis[col[k]] = false; }} return ans; } bool Dfs (int d) {//pruning if (d + f () > Best) return false; R[0] = 0 means to find a viable solution if (r[0] = = 0) return d <= best; Find the smallest column of s, speed up the search by int c = r[0]; For (i, R, 0) if (S[i] < s[c]) C = i; For (I, D, c) {remove (i); For (J, R, I) remove (j); if (Dfs (d + 1)) return true; For (J, R , i) restore (j); Restore (i); } return false; } bool Solve (int k) {best = k; Return DFS (0); } int best; } DLX; int n, m, K; __int64 ABS (__int64 x) {return x < 0?-x:x;} struct point{__int64 x, y; void Get () {scanf ("%i64d%i64d", &x, &y); } Friend __int64 Dist (const point &a, const Point & B) {return abs (a.x-b.x) + ABS (A.Y-B.Y);} }; Point city[65]; __int64 dis[60*60+10]; int Lisanhua (int Index) {int cnt = 1; for (int i = 1; i < Index; i + +) {if (dis[i]! = Dis[i -1]) dis[cnt++] = dis[i];} return cnt-1;} int main () {int cas; int casn = 1; scanf ("%d", &cas); while (CAS--) {scanf ("%d%d", &n, &k); for (int i = 1; I <= n; i + +) {city[i].get (); } int Index = 0; for (int i = 1; I <= n; i + +) {for (int j = i; J <= N; j + +) {dis[index++] = dist (City[i], CITY[J]);}} Sort (dis, dis + index); index = Lisanhua (index); __int64 L = 0, R = Index; __int64 mid; while (L < r) {mid = (L + r)/2; Dlx.init (n); for (int i = 1; I <= n; i + +) {for (int j = 1; J <= N; j + +) {if (Dist (city[i], city[j]) <= Dis[mid] ) {DLX. Link (i, j); }}} if (Dlx.solve (k)) {r = Mid; }else{L = mid + 1; }} printf ("Case #%d:%i64d\n", casn + +, dis[l]); } return 0; }
HDU 5046 Airport (Dancing Links repeatedly covered)