kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

dancing links code 4-5

四、hdu 3663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

using namespace std;

int n,m,DD,a[66][66],s[66],f[66],id[66][6];

const int MAXN = 1005;

int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN], Row[MAXN*MAXN],Ans[MAXN],ans,limit,up;


void Remove(int c) {
	L[R[c]] = L[c];
	R[L[c]] = R[c];
	for (int i = D[c]; i != c; i = D[i])
	for (int j = R[i]; j != i; j = R[j]) {
	    U[D[j]] = U[j];
	    D[U[j]] = D[j];
	    -- S[Col[j]];
	}
}
void Resume(int c) {
	for (int i = U[c]; i != c; i = U[i])
	for (int j = L[i]; j != i; j = L[j]) {
	    U[D[j]] = j;
	    D[U[j]] = j;
	    ++ S[Col[j]];
	}
	L[R[c]] = c;
	R[L[c]] = c;
}

bool dfs(int depth) {
//    printf("ddd = %d  R0 = %d\n",depth,R[0]);
	if(R[0] == 0) { if(depth > ans)ans = depth; return true; }
	int i, j, c, minnum = 1000000000;
	for (i = R[0]; i != 0; i = R[i]) {
	    if (S[i] < minnum) {
	        minnum = S[i];
	        c = i;
	    }
	}
	Remove(c);
	for (i = U[c]; i != c; i = U[i]) {
	    Ans[depth] = Row[i];
	    for (j = R[i]; j != i; j = R[j]) Remove(Col[j]);
	    if (dfs(depth + 1)) return true;
	    for (j = L[i]; j != i; j = L[j]) Resume(Col[j]);
	}
	Resume(c);
	return false;
}
int solve(int n, int m, int DL[][MAXN]) {
	for (int i = 0; i <= m; i ++) {
	    L[i] = i - 1;
	    R[i] = i + 1;
	    U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
	    int head = cnt, tail = cnt;
	    for (int c = 1; c <= m; c ++) if (DL[i][c]) {
	        S[c] ++;
	        Row[cnt] = i;
	        Col[cnt] = c;
	        U[D[c]] = cnt;
	        D[cnt] = D[c];
	        U[cnt] = c;
	        D[c] = cnt;
	        L[cnt] = tail;
	        R[tail] = cnt;
	        R[cnt] = head;
	        L[head] = cnt;
	        tail = cnt;
	        cnt ++;
	    }
	}
	if (dfs(0)) return true;
	return false;
}


int mark[MAXN][MAXN],x[MAXN],y[MAXN],z[MAXN];

int main()
{
	int i,j,k,l,row,col,g;
	while(scanf("%d %d %d",&n,&m,&DD) != EOF)
	{
	    for(i=1;i<=n;i++)
	        for(j=1;j<=n;j++)
	        {
	            a[i][j] = 0;
	            if(i == j)a[i][j] = 1;
	        }
	    while(m--) {
	        scanf("%d %d",&j,&k);
	        a[j][k] = a[k][j] = 1;
	    }
	    for(i=1;i<=n;i++)scanf("%d %d",&s[i],&f[i]);
	   
	    col = 0;
	    for(i=1;i<=n;i++)
	        for(j=1;j<=DD;j++)
	            id[i][j] = ++col;
	   
	    row = 0;
	    for(i=1;i<=n;i++)
	    {
	        for(j=s[i];j<=f[i];j++)
	            for(k=j;k<=f[i];k++)
	            {
	                row++; x[row] = j; y[row] = k; z[row] = i;
	                for(l=1;l<=col;l++)mark[row][l] = 0;
	               
	                for(l=1;l<=n;l++)if(a[i][l] == 1)
	                {
	                    for(g=j;g<=k;g++) mark[row][id[l][g]] = 1;
	                }
	            }
	    }
	   
	    int rr=0;
	   
	    for(i=1;i<=n;i++)
	    {
	        col++;
	        for(j=1;j<=row;j++)mark[j][col] = 0;
	       
	        for(j=s[i];j<=f[i];j++)
	            for(k=j;k<=f[i];k++)
	            {
	                rr++;
	                mark[rr][col] = 1;
	            }
	    }
	   
	    int TT = row;
	    for(i=1;i<=n;i++)
	    {
	        row++;
	        for(j=1;j<=col;j++)mark[row][j] = 0;
	        mark[row][col-n+i] = 1;
	    }
	   
	    ans = 0;
	   
	    k = solve(row, col, mark);
	   
	    if(k == 0)
	        printf("No solution\n");
	    else
	    {
	        for(i=1;i<=n;i++)s[i] = f[i] = 0;
	        for(i=0;i<ans;i++)
	        if(Ans[i] <= TT)
	        {
	            s[z[Ans[i]]] = x[Ans[i]];
	            f[z[Ans[i]]] = y[Ans[i]];
	        }
	        for(i=1;i<=n;i++)printf("%d %d\n",s[i],f[i]);
	    }
	    printf("\n");
	}
	return 0;
}
五、hdu 2995
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
		L[R[i]] = L[i];
		R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
		L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
			for (int j = R[i]; j != i; j = R[j])
				hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = 2000000000;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
		Remove(i);
		for (j = R[i]; j != i; j = R[j]) Remove(j);
		if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	int best = 0, worst = maxdepth;
	/*while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}*/
	limit = maxdepth;
	if(dfs(0))best = maxdepth;
	else
		best = maxdepth+1;
	return best;
}

int x[155],y[155];

int dij(int i, int j)
{
	int d1 = (x[i]-x[j])*(x[i]-x[j]);
	int d2 = (y[i]-y[j])*(y[i]-y[j]);
	return d1+d2;
}

int main()
{
	int i,j,k,l,row,col,n,m,low,up,mid,mark[MAXN][MAXN],d[55][55],T,b[3000],top;
 
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d %d %d",&n,&row,&m);

		for(i=1;i<=n+row;i++)
			scanf("%d %d",&x[i],&y[i]);
 
		low = 1; up = 1000000000;

		while(low < up)
		{
			for(i=1;i<=row;i++)
				for(j=1;j<=n;j++)mark[i][j] = 0;
   
			mid = (low + up)>>1;
			for(i=1;i<=row;i++)
				for(j=1;j<=n;j++)if(dij(i+n,j) <= mid)mark[i][j] = 1;
	
			if(solve(row, n, mark, m) > m)low = mid+1; else up = mid;
		}
		mid = (low + up)>>1;
		printf("%.6lf\n",sqrt(1.0*mid));
	}
	return 0;
}

dancing links code 1-3

一、fzu 1686
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<stdio.h>
#include<algorithm>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
		L[R[i]] = L[i];
		R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
		L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
			for (int j = R[i]; j != i; j = R[j])
				hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = INT_MAX;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
		Remove(i);
		for (j = R[i]; j != i; j = R[j]) Remove(j);
		if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	int best = 0, worst = maxdepth;
	while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}
	return best;
}

int main()
{
	int i,j,k,l,row,col,n,m,n1,m1,mark[MAXN][MAXN],a[33][33],id[33][33];
 
	while(scanf("%d %d",&n,&m) != EOF)
	{
		col = 0;
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);
				id[i][j] = -1;
				if(a[i][j] == 1) { col++; id[i][j] = col; }
			}
		scanf("%d %d",&n1,&m1);

		row = 0;
		for(i=1;i<=n-n1+1;i++)
			for(j=1;j<=m-m1+1;j++)
			{
				row++;
				for(k=1;k<=col;k++)mark[row][k] = 0;
 
				for(k=i;k<i+n1;k++)
					for(l=j;l<j+m1;l++)
					if(id[k][l] > 0)
						mark[row][id[k][l]] = 1;
			}
 
		printf("%d\n",solve(row, col, mark, row));
	}
	return 0;
}

二、zju 3209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

using namespace std;

const int MAXN = 1005;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN], Row[MAXN*MAXN],ans,limit,up;


void Remove(int c) {
	L[R[c]] = L[c];
	R[L[c]] = R[c];
	for (int i = D[c]; i != c; i = D[i])
	for (int j = R[i]; j != i; j = R[j]) {
		U[D[j]] = U[j];
		D[U[j]] = D[j];
		-- S[Col[j]];
	}
}
void Resume(int c) {
	for (int i = U[c]; i != c; i = U[i])
	for (int j = L[i]; j != i; j = L[j]) {
		U[D[j]] = j;
		D[U[j]] = j;
		++ S[Col[j]];
	}
	L[R[c]] = c;
	R[L[c]] = c;
}

bool dfs(int depth) {
	if (depth >= ans) return true;
 
	if(R[0] == 0) { if(depth < ans)ans = depth;  return true; }
 
	int i, j, c, minnum = 2000000000, flag = 0;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	Remove(c);
	for (i = U[c]; i != c; i = U[i]) {
		//如果需要的话,在这里记录一组解(Ans[depth] = Row[i])
		for (j = R[i]; j != i; j = R[j]) Remove(Col[j]);
		if (dfs(depth + 1)) flag = 1; //return true;
		for (j = L[i]; j != i; j = L[j]) Resume(Col[j]);
	}
	Resume(c);
	return flag;//false;
}
int solve(int n, int m, int DL[][MAXN]) {
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Row[cnt] = i;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	if (dfs(0)) return true;
	return false;
}

int mark[MAXN][MAXN];

int main()
{
	int i,j,k,l,n,m,T,row,col,x1,x2,y1,y2,id[33][33],low;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d %d %d",&n,&m,&row);
		col = 0;
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				col++; id[i][j] = col;
			}
 
		for(i=1;i<=row;i++)
			for(j=1;j<=col;j++)mark[i][j] = 0;

		for(i=1;i<=row;i++)
		{
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
			x1++; y1++;
			for(k=x1;k<=x2;k++)
				for(l=y1;l<=y2;l++)
				mark[i][id[k][l]] = 1;
		}

		ans = 1000000000;
		if(!solve(row, col, mark))ans = -1;

		printf("%d\n",ans);
	}
	return 0;
}

三、hdu 3529
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<stdio.h>
#include<algorithm>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
		L[R[i]] = L[i];
		R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
		L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
		for (int j = R[i]; j != i; j = R[j])
		hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = 2000000000;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
		Remove(i);
		for (j = R[i]; j != i; j = R[j]) Remove(j);
		if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
			L[i] = i - 1;
			R[i] = i + 1;
			U[i] = D[i] = i;
		}
		L[0] = m;
		R[m] = 0;
		int cnt = m + 1;
		memset(S, 0, sizeof (S));
		for (int i = 1; i <= n; i ++) {
			int head = cnt, tail = cnt;
			for (int c = 1; c <= m; c ++) if (DL[i][c]) {
				S[c] ++;
				Col[cnt] = c;
				U[D[c]] = cnt;
				D[cnt] = D[c];
				U[cnt] = c;
				D[c] = cnt;
				L[cnt] = tail;
				R[tail] = cnt;
				R[cnt] = head;
				L[head] = cnt;
				tail = cnt;
				cnt ++;
			}
		}
		int best = 0, worst = maxdepth;
		while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}
	return best;
}

int n,m,M[MAXN][MAXN];

int main()
{
	int i,j,k,l,row,col,idr[33][33],idc[33][33];
   
	char ch[33][33];
	while(scanf("%d %d",&n,&m) != EOF)
	{
		for(i=1;i<=n;i++) scanf("%s",ch[i]+1);
	   
		row = col = 0;
	   
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			if(ch[i][j] == '.') idr[i][j] = ++row;
			else
			if(ch[i][j] == '#') idc[i][j] = ++col;
		   
		for(i=0;i<=row;i++) for(j=0;j<=col;j++) M[i][j] = 0;
	   
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			if(ch[i][j] == '.')
			{
				k = i-1; l = j; while(k > 0 && ch[k][l] == '.') k--;
				if(k > 0 && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			   
				k = i+1; l = j; while(k <= n && ch[k][l] == '.') k++;
				if(k <= n && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			   
				k = i; l = j-1; while(l > 0 && ch[k][l] == '.') l--;
				if(l > 0 && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			   
				k = i; l = j+1; while(l <= m && ch[k][l] == '.') l++;
				if(l <= m && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			}
		   
		int ans = solve(row, col, M, col);
		printf("%d\n",ans);
	}
	return 0;  
}

dancing links

Knuth Dancing_Links 中文版 http://www.docin.com/p-31928825.html

http://acm.fzu.edu.cn/problem.php?pid=1686

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3209

http://acm.hdu.edu.cn/showproblem.php?pid=3529

http://acm.hdu.edu.cn/showproblem.php?pid=3663

http://acm.hdu.edu.cn/showproblem.php?pid=2295

http://poj.org/problem?id=3074

http://poj.org/problem?id=3076

// fzu 1686
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<stdio.h>
#include<algorithm>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
	L[R[i]] = L[i];
	R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
	L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
		for (int j = R[i]; j != i; j = R[j])
		hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = INT_MAX;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
			Remove(i);
			for (j = R[i]; j != i; j = R[j]) Remove(j);
			if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	int best = 0, worst = maxdepth;
	while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}
	return best;
}

int main()
{
	int i,j,k,l,row,col,n,m,n1,m1,mark[MAXN][MAXN],a[33][33],id[33][33];

	while(scanf("%d %d",&n,&m) != EOF)
	{
		col = 0;
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);
				id[i][j] = -1;
				if(a[i][j] == 1) { col++; id[i][j] = col; }
			}
		scanf("%d %d",&n1,&m1);

		row = 0;
		for(i=1;i<=n-n1+1;i++)
			for(j=1;j<=m-m1+1;j++)
			{
				row++;
				for(k=1;k<=col;k++)mark[row][k] = 0;
			
				for(k=i;k<i+n1;k++)
					for(l=j;l<j+m1;l++)
					if(id[k][l] > 0)
						mark[row][id[k][l]] = 1;
			}
		
		printf("%d\n",solve(row, col, mark, row));
	}
	return 0;
}
// hdu 2295
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
	L[R[i]] = L[i];
	R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
	L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
		for (int j = R[i]; j != i; j = R[j])
		hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = 2000000000;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
			Remove(i);
			for (j = R[i]; j != i; j = R[j]) Remove(j);
			if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	int best = 0, worst = maxdepth;
	/*while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}*/
	limit = maxdepth;
	if(dfs(0))best = maxdepth;
	else
		best = maxdepth+1;
	return best;
}

int x[155],y[155];

int dij(int i, int j)
{
	int d1 = (x[i]-x[j])*(x[i]-x[j]);
	int d2 = (y[i]-y[j])*(y[i]-y[j]);
	return d1+d2;
}

int main()
{
	int i,j,k,l,row,col,n,m,low,up,mid,mark[MAXN][MAXN],d[55][55],T,b[3000],top;


	scanf("%d",&T);
	while(T--)
	{
		scanf("%d %d %d",&n,&row,&m);

		for(i=1;i<=n+row;i++)
			scanf("%d %d",&x[i],&y[i]);
	
		low = 1; up = 1000000000;

		while(low < up)
		{
			for(i=1;i<=row;i++)
				for(j=1;j<=n;j++)mark[i][j] = 0;
		
			mid = (low + up)>>1;
			for(i=1;i<=row;i++)
				for(j=1;j<=n;j++)if(dij(i+n,j) <= mid)mark[i][j] = 1;
			
			if(solve(row, n, mark, m) > m)low = mid+1; else up = mid;
		}
		mid = (low + up)>>1;
		printf("%.6lf\n",sqrt(1.0*mid));
	}
	return 0;
}