HDU 2295 radar (dual + dancing links duplicate overwrite)

Source: Internet
Author: User
Tags radar

There are n cities and M stations. You need to select K stations. Each Station draws a circle with a radius of R to cover all the cities, A city can be covered by multiple sites. This is the minimum R that meets the requirements.

The idea is obvious. The first is to divide the problem into a feasibility decision.

For mid, we regard the station as a row, and the city as a column. If the distance between a station and a city is smaller than the mid, the corresponding matrix position is 1; otherwise, it is 0, solve with repeated DLX Overwrite

There are two main differences between repeated coverage and precise coverage:

First, remove and restore functions are different. Duplicate overwriting does not need to delete rows when deleting columns.

Second, because the row is not deleted, the matrix become sparse and the speed will be slowed down. The heuristic function is required for pruning.


The heuristic function selects a column, deletes (in fact traverses) All rows of column 1, and then marks out the columns with 1 in these rows.

The cost of deleting multiple rows is only 1, so it will cost less than the actual cost. If the current depth plus the estimated value that is better than the optimal solution is not feasible, it will certainly be cut off.


# Include <stdio. h> # include <string. h> # include <algorithm> # include <vector> # include <math. h> using namespace STD; const int maxn = 55 + 10; const int maxr = 55 + 10; const int maxnode = 55*55 + maxr + 10; # define for (I, a, S) for (INT I = A [s]; I! = S; I = A [I]) struct DLX {// Number of maxn columns, total number of maxnode nodes, number of 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 virtual node of the first line 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; // number of each column memset (S, 0, sizeof (s )); // H [I] =-1 indicates that this row does not have 1 // otherwise, it indicates the number of memset (H,-1, sizeof (H) for the Sz of the first 1 ));} // Add a 1 void Link (int r, int c) {row [SZ] = r; Col [SZ] = C in Column C of row R; 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] ;}} // restore 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 indicates finding a feasible solution if (R [0] = 0) return d <= best; // locate the column with the smallest s value, accelerate search speed 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; struct point {Double X, Y; void get () {scanf ("% lf", & X, & Y);} friend double dist (const point & A, const point & B) {return SQRT (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y) ;}}; Point City [55]; point station [55]; int main () {int CAS; scanf ("% d", & CAS ); while (CAS --) {scanf ("% d", & N, & M, & K); For (INT I = 1; I <= N; I ++) {City [I]. get () ;}for (INT I = 1; I <= m; I ++) {station [I]. get ();} double L = 0.0, r = 2000.0; double mid; while (R-l> 1e-8) {mid = (L + r)/2.0; DLX. init (n); For (INT I = 1; I <= m; I ++) {for (Int J = 1; j <= N; j ++) {If (Dist (station [I], City [J]) <= mid) {DLX. link (I, j) ;}} if (DLX. solve (k) {r = mid;} else {L = mid;} printf ("% lf \ n", mid);} return 0 ;}


HDU 2295 radar (dual + dancing links duplicate overwrite)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.