HDU 4735 DLX爆搜

Little Wish~ lyrical step~

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 417    Accepted Submission(s): 109

Problem Description

N children are living in a tree with exactly N nodes, on each node there lies either a boy or a girl.

A girl is said to be protected, if the distance between the girl and her nearest boy is no more than D.

You want to do something good, so that each girl on the tree will be protected. On each step, you can choose two nodes, and swap the children living on them.

What is the minimum number of steps you have to take to fulfill your wish?

Input

The first line has a number T (T <= 150) , indicating the number of test cases.

In a case, the first line contain two number n (1 <= n <= 50), D (1 <= D <= 10000000), Which means the number of the node and the distance between the girls and boys.

The next lines contains n number. The ith number means the ith node contains a girl or a boy. (0 means girl 1 means boy), The follow n - 1 lines contains a, b, w, means a edge connect ath node and bth node, and the
length of the edge is w (1 <= w <= 10000000).

Output

For every case, you should output "Case #t: " at first, without quotes. The
t is the case number starting from 1.

Then follows the answer, -1 meas you can‘t comlete it, and others means the minimum number of the times.

Sample Input

1
3 1
0 0 1
1 2 1
1 3 1

Sample Output

Case #1: 1

Source

2013 ACM/ICPC Asia Regional Chengdu Online

一棵树n个节点,每一个节点或者有男孩,或者有女孩,每一个男孩可以保护距离他D范围之内的男孩,问最少通过多少次交换可以使得所有女孩受到保护。

男孩与男孩,女孩与女孩之间的交换显然毫无意义,只用考虑男孩与女孩之间的交换,可以DP做,也可以DLX爆搜,只考虑男孩,女孩毫无意义,可以先DLX爆搜得到解,然后判断需要多少次交换,当搜到一组解时,里面的女孩个数就是需要交换的次数,我们只需要把女孩全部移走,换成男孩,就可以保证把所有点覆盖。

代码:

/* ***********************************************
Author :_rabbit
Created Time :2014/5/1 22:56:26
File Name :12.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
int boys,boy[100],d[60][60];
struct DLX{
    const static int maxn=7500;
    #define FF(i,A,s) for(int i = A[s];i != s;i = A[i])
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int size,col[maxn],row[maxn],s[maxn],H[100];
    bool vis[100];
	int ans[maxn],cnt;
    void init(int m){
        for(int i=0;i<=m;i++){
            L[i]=i-1;R[i]=i+1;U[i]=D[i]=i;s[i]=0;
        }
        memset(H,-1,sizeof(H));
        L[0]=m;R[m]=0;size=m+1;
    }
	void link(int r,int c){
         U[size]=c;D[size]=D[c];U[D[c]]=size;D[c]=size;
         if(H[r]<0)H[r]=L[size]=R[size]=size;
         else {
             L[size]=H[r];R[size]=R[H[r]];
             L[R[H[r]]]=size;R[H[r]]=size;
         }
         s[c]++;col[size]=c;row[size]=r;size++;
     }
	void del(int c){//精确覆盖
        L[R[c]]=L[c];R[L[c]]=R[c];
        FF(i,D,c)FF(j,R,i)U[D[j]]=U[j],D[U[j]]=D[j],--s[col[j]];
    }
    void add(int c){  //精确覆盖
        R[L[c]]=L[R[c]]=c;
        FF(i,U,c)FF(j,L,i)++s[col[U[D[j]]=D[U[j]]=j]];
    }
	bool dfs(int k){//精确覆盖
        if(!R[0]){
            cnt=k;return 1;
        }
        int c=R[0];FF(i,R,0)if(s[c]>s[i])c=i;
        del(c);
        FF(i,D,c){
            FF(j,R,i)del(col[j]);
            ans[k]=row[i];if(dfs(k+1))return true;
            FF(j,L,i)add(col[j]);
        }
        add(c);
        return 0;
    }
    void remove(int c){//重复覆盖
        FF(i,D,c)L[R[i]]=L[i],R[L[i]]=R[i];
    }
     void resume(int c){//重复覆盖
         FF(i,U,c)L[R[i]]=R[L[i]]=i;
     }
    int A(){//估价函数
        int res=0;
        memset(vis,0,sizeof(vis));
        FF(i,R,0)if(!vis[i]){
                res++;vis[i]=1;
                FF(j,D,i)FF(k,R,j)vis[col[k]]=1;
            }
        return res;
    }
    void dfs(int now,int &lim){//重复覆盖
		if(now+A()>boys)return;int tt=0;
		for(int i=0;i<now;i++)tt+=boy[ans[i]];
		if(now-tt>=lim)return;
		if(!R[0]){
			lim=now-tt;
			return;
		}
        int temp=INF,c;
        FF(i,R,0)if(temp>=s[i])temp=s[i],c=i;
        FF(i,D,c){
			ans[now]=row[i];
            remove(i);FF(j,R,i)remove(j);
            dfs(now+1,lim);
            FF(j,L,i)resume(j);resume(i);
        }
    }
}dlx;

int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int T;
	 cin>>T;
	 for(int t=1;t<=T;t++){
		 int n,D;boys=0;
		 scanf("%d%d",&n,&D);
		 dlx.init(n);
		 for(int i=1;i<=n;i++)scanf("%d",&boy[i]),boys+=boy[i];
		 for(int i=1;i<=n;i++)
			 for(int j=1;j<=n;j++)
				 d[i][j]=i==j?0:INF;
		 for(int i=1;i<n;i++){
			 int u,v,p;
			 scanf("%d%d%d",&u,&v,&p);
			 d[u][v]=d[v][u]=p;
		 }
		 for(int k=1;k<=n;k++)
			 for(int i=1;i<=n;i++)
				 for(int j=1;j<=n;j++)
					 if(d[i][j]>d[i][k]+d[k][j])d[i][j]=d[i][k]+d[k][j];
		 for(int i=1;i<=n;i++)
			 for(int j=1;j<=n;j++)
				 if(d[i][j]<=D)dlx.link(i,j);
		 int ans=INF;
		 dlx.dfs(0,ans);
		 if(ans>boys)ans=-1;
		 printf("Case #%d: %d\n",t,ans);
	 }
     return 0;
}

HDU 4735 DLX爆搜,布布扣,bubuko.com

时间: 2024-10-23 13:39:28

HDU 4735 DLX爆搜的相关文章

hdu 5031 Lines 爆搜

其实嘞,这个线可以只延伸一端 然后嘞,爆搜一次就可以 最后嘞,600-800ms过 本弱就是弱啊,你来打我呀-- #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int a[100][100]; int n,m,ans; bool dfs(int step) { int i,j,t,ii,jj,x,y,cnt,tx,t

HDU 5025 水爆搜

2014 ACM/ICPC Asia Regional Guangzhou Online 水爆搜 N*N矩阵,找最长的一条路径,使'.'最多,路径可以且最多可以转一次90°. 枚举每个点,枚举8方向连续的'.'有多少个,再枚举路径方式. #include "stdio.h" #include "string.h" int main() { int n,ans,i,j,k; int sum[9]; char str[101][101]; while (scanf(&q

HDU 2295 DLX 二分

Radar Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2229    Accepted Submission(s): 888 Problem Description N cities of the Java Kingdom need to be covered by radars for being in a state of w

hdu 4735 Little Wish~ lyrical step~(DLX)

题目链接:hdu 4735 Little Wish~ lyrical step~ 题意: 有n个节点的树,每个节点可能是男孩,可能是女孩,节点之间有距离,现在要让所有的女孩周围距离D之内有男孩,问最小需要交换多少次男孩和女孩的位置. 题解: 把每个节点对小于D距离的全部link起来,然后DLX爆艹,意义就是选n个节点去覆盖全部节点,如果这个节点是女生,那么就是要替换的点.然后不断更新答案. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int

HDU 4403 A very hard Aoshu problem(dfs爆搜)

http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比较小,直接dfs爆搜答案即可. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<map> 6 using nam

HDU 3316 爆搜水题

爆搜水题 模拟扫雷,规则和扫雷一样 给出原图,求在X,Y位置点一下以后的图形,没有弹出的点输出-1,弹出的点输出这个点的数字 从起始点DFS一下即可 #include "stdio.h" #include "string.h" int dir[8][2]={ {-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1} }; int n; int hash[110][110]; char str[110][110]; i

hdu5355 思维+爆搜

pid=5355">http://acm.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are?m?soda and today is their birthday. The?1-st soda has prepared?n?cakes with size?1,2,-,n. Now?1-st soda wants to divide the cakes into?m?parts so that the total

8/2 multi4 E找规律+模拟,空间开小了然后一直WA。。。J爆搜check不严谨WA。。。multi3 G凸包判共线写错数组名???样例太好过.想哭jpg。

multi4 Problem E. Matrix from Arrays 题意:构造一个数组,求子矩阵前缀和. 思路:打表找规律,"发现"L为奇数时循环节为L,为偶数时循环节为2L,求相应循环节的二维前缀和然后加加减减计算一下就好. 虚伪地证明一下循环节:L为奇数时对于第x行/列开始的位置有(x  +  x+L-1)*L/2   ->     (2x+L-1)/2(为整数)*L,因此扫过L行/列也就扫过了L整数倍"(2x+L-1)/2"倍的A[0]~A[L],

P1074 靶形数独(爆搜)

说是爆搜,却还是有点技巧 首先数独应该有个想法,从最有可能填上数字的那些地方开始 也就是说哪些行0最少,就从那儿开始 为了最大限度地利用已有信息,就先把所有0的位置扣出来单独填 最后别忘了回溯就行 但是这种做法仍然过不了下面这个样例: 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0