Description

Input

Output

 

Sample Input

3
8 7 6
3 9 4
1 10 5

Sample Output

18

Data Constraint

 
做法:几乎一看数据范围就可以猜出状态压缩dp,然而不知道为什么我打炸了。。于是该打记忆化搜索。
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #define rep(i,a,b) for (int i=1;i<=n;i++)
 5 #define M 65537
 6 #define N 17
 7 using namespace std;
 8 int a[N],b[N],c[N],n,ans;
 9 int f[M][N][3];
10 
11 void Init(){
12     scanf("%d",&n);
13     rep(i,1,n)    scanf("%d%d%d",&a[i],&b[i],&c[i]);
14 }
15 
16 void Dfs(int dep,int x,int zt,int situ,int high){
17     if (f[situ][x][zt]>=high) return;
18     else f[situ][x][zt]=high,ans=max(ans,high);
19     if (dep>n){
20         ans=max(ans,high);
21         return;    
22     } 
23     int cmpx,cmpy;
24     if (zt==0||zt==1) cmpx=a[x]; else cmpx=b[x];
25     if (zt==1||zt==2) cmpy=c[x]; else cmpy=b[x];
26     if (cmpx<cmpy) swap(cmpx,cmpy);
27     rep(i,1,n){
28         if (1<<i&situ) continue;
29         int xx=a[i],yy=b[i],next=0;
30         if (xx<yy) swap(xx,yy);
31         if (xx<=cmpx&&yy<=cmpy) Dfs(dep+1,i,next,situ|(1<<i),high+c[i]);
32         
33         xx=a[i],yy=c[i],next=1;
34         if (xx<yy) swap(xx,yy);
35         if (xx<=cmpx&&yy<=cmpy) Dfs(dep+1,i,next,situ|(1<<i),high+b[i]);
36         
37         xx=b[i],yy=c[i],next=2;
38         if (xx<yy)    swap(xx,yy);
39         if (xx<=cmpx&&yy<=cmpy) Dfs(dep+1,i,next,situ|(1<<i),high+a[i]);        
40     }
41 }
42 
43 void Work(){
44     rep(i,1,n){
45         Dfs(1,i,0,1<<i,c[i]);
46         Dfs(1,i,1,1<<i,b[i]);
47         Dfs(1,i,2,1<<i,a[i]);
48     }
49     printf("%d",ans);
50 }
51 
52 int main(){
53     Init();
54     Work();
55 }
56  
View Code