HDU 2295 (the bare DLX times out and requires pruning)

Source: Internet
Author: User
Tags radar

 

When Judge () is used, if depth + judge () is greater than count, false is directly returned;

 

# Include <iostream>

# Include <cmath>

# Include <stdio. h>

Using namespace std;

 

Const int n= 100;

Struct node {

Int L, R, D, U, col;

} Dia [N * N];

Double map [N] [N];

Int Size [N];

 

Pair <double, double> city [N], Radar [N];

 

Int n, m, count;

Void makegraph (double mid)

{

Int I, j, now, pre, RP;

///// Initialize the header pointer ///////////

For (I = 0; I <= n; I ++)

{

Dia [I]. col = I;

Dia [I]. D = dia [I]. U = I;

Dia [I]. L = I-1;

Dia [I]. R = I + 1;

Size [I] = 0;

}

Dia [0]. L = n; dia [n]. R = 0;

Now = n + 1;

For (I = 1; I <= m; I ++)

{

RP = pre = now; // RP row pointer

For (j = 1; j <= n; j ++)

If (map [I] [j] <mid ){

Dia [now]. col = j; // column number

Size [j] ++;

 

Dia [now]. U = dia [j]. U;

Dia [now]. D = j;

Dia [dia [j]. U]. D = now;

Dia [j]. U = now;

 

Dia [now]. L = pre;

Dia [now]. R = RP;

Dia [pre]. R = now;

Dia [RP]. L = now;

Pre = now ++;

}

}

}

 

Void Cover (int c)

{

// Dia [dia [c]. R]. L = dia [c]. L;

// Dia [dia [c]. L]. R = dia [c]. R;

For (int I = dia [c]. D; I! = C; I = dia [I]. D)

{

Dia [dia [I]. L]. R = dia [I]. R;

Dia [dia [I]. R]. L = dia [I]. L;

Size [dia [I]. col] --;

}

}

Void Recover (int c)

{

For (int I = dia [c]. U; I! = C; I = dia [I]. U)

// For (int j = dia [I]. L; j! = I; j = dia [j]. L)

{

Dia [dia [I]. L]. R = I;

Dia [dia [I]. R]. L = I;

Size [dia [I]. col] ++;

}

// Dia [dia [c]. R]. L = c;

// Dia [dia [c]. L]. R = c;

}

 

Int judge ()

{

Int hash [N] = {0 };

Int ans = 0, I, j, k;

For (I = dia [0]. R; I = dia [I]. R)

If (! Hash [I])

{

Ans ++;

Hash [I] = 1;

For (j = dia [I]. D; j! = I; j = dia [j]. D)

For (k = dia [j]. R; k! = J; k = dia [k]. R)

Hash [dia [k]. col] = 1;

}

Return ans;

}

Bool Algorithm_X (int depth)

{

Int I, j;

If (depth + judge ()> count)

// If (depth> count)

Return false;

If (dia [0]. R = 0)

Return true;

 

Int mi = 0 xfffffff, c_pos;

For (I = dia [0]. R; I! = 0; I = dia [I]. R)

If (mi> Size [I]) {

Mi = Size [I];

C_pos = I;

If (mi = 1) break;

}

// If (Size [c_pos] = 0) return false;

// Cover (c_pos );

For (I = dia [c_pos]. D; I! = C_pos; I = dia [I]. D)

{

Cover (I );

For (j = dia [I]. R; j! = I; j = dia [j]. R)

Cover (j );

If (Algorithm_X (depth + 1 ))

Return true;

For (j = dia [I]. L; j! = I; j = dia [j]. L)

Recover (j );

Recover (I );

}

// Recover (c_pos );

Return false;

}

 

Void Init (double & ma)

{

Int I, j;

For (I = 1; I <= m; I ++)

For (j = 1; j <= n; j ++)

{

Map [I] [j] = sqrt (city [j]. first-Radar [I]. first) * (city [j]. first-Radar [I]. first)

+ (City [j]. second-Radar [I]. second) * (city [j]. second-Radar [I]. second ));

Ma = max (ma, map [I] [j]);

}

}

 

Void print (double mid)

{

For (int I = 1; I <= n; I ++)

{

For (int j = 1; j <= m; j ++)

If (map [I] [j] <= mid)

{

Printf ("1 ");

}

Else

Printf ("0 ");

Printf ("/n ");

}

}

 

Int main ()

{

Int I, j;

Int T;

Scanf ("% d", & T );

While (T --)

{

// Memset (Size, 0, sizeof (Size)

Scanf ("% d", & n, & m, & count );

For (I = 1; I <= n; I ++)

Scanf ("% lf", & city [I]. first, & city [I]. second );

For (I = 1; I <= m; I ++)

Scanf ("% lf", & Radar [I]. first, & Radar [I]. second );

Double ma = 0;

Init (ma );

Double low = 0, high = ma + 1, mid;

While (low + 1e-8 <= high)

{

Mid = (low + high)/2;

// Mid = 3;

// Printf ("mid = % lf/n", mid );

Makegraph (mid );

// Print (mid );

Bool flag = true;

/* For (I = dia [0]. R; I! = 0; I = dia [I]. R)

If (Size [I] = 0 ){

Flag = false;

Break;

}*/

If (flag & Algorithm_X (0 ))

High = mid;

Else

Low = mid;

}

Printf ("%. 6lf/n", low );

}

Return 0;

}

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.