hdu 4277 2012长春赛区网络赛 dfs+hashmap ***

hashmap判重大法好

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
int sum=,a[MAXN];
const int HASH = ;
struct HASHMAP
{
int head[HASH],next[HASH],size;
int state[HASH][];
int hash(int a,int b,int c)
{
return (((a*+b)*+c)*)%HASH;  //131
}
void init()
{
size = ;
memset(head,-,sizeof(head));
}
int insert(int a,int b,int c)
{
int h = hash(a,b,c);
for(int i = head[h]; i != -;i = next[i])
if(a == state[i][]&&b == state[i][]&&c == state[i][])
{
return ;
}
state[size][]=a,state[size][]=b,state[size][]=c;
next[size] = head[h];
head[h] = size++;
return ;
}
} H1;
bool check(int a1,int a2,int a3)
{
if(a1+a2>a3&&a1+a3>a2&&a2+a3>a1) return ;
else return ;
}
void dfs(int a1,int a2,int a3,int pos)
{
if(a3>sum/) return;
if(pos>=n)
{
if(a1==||a2==||a3==) return;
if(check(a1,a2,a3)&&a1>=a2&&a2>=a3)
{
H1.insert(a1,a2,a3);
}
return;
}
dfs(a1+a[pos],a2,a3,pos+);
dfs(a1,a2+a[pos],a3,pos+);
dfs(a1,a2,a3+a[pos],pos+);
} int main()
{
int i,j,k,ca=;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
while(tt--)
{
H1.init();
sum=;
scanf("%d",&n);
for(i=;i<n;i++) scanf("%d",a+i),sum+=a[i];
dfs(,,,);
printf("%d\n",H1.size);
}
}
上一篇:【转】Select模型原理


下一篇:日均5亿查询量的京东订单中心,为什么舍MySQL用ES?