hdu3270Arranging Your Team(dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=3720
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<map> 8 #include<string> 9 using namespace std; 10 #define INF 0x3f3f3f 11 map<string,int>f; 12 struct node 13 { 14 char s[101],po[35]; 15 int v,k; 16 }p[30]; 17 int pa[30],flag,w[30][30],maxz; 18 void dfs(int x,int a,int b,int c,int d,int sum) 19 { 20 int i,j; 21 if(x==24&&a==4&&b==4&&c==2&&d==1) 22 { 23 for(i =1; i <= 11 ; i++) 24 for(j = i+1 ; j <= 11 ; j++) 25 sum+=w[pa[i]][pa[j]]; 26 maxz = max(maxz,sum); 27 flag = 1; 28 return ; 29 } 30 if(x>23) return ; 31 pa[a+b+c+d+1] = x; 32 if(p[x].k == 4) 33 { 34 if(d<1) 35 dfs(x+1,a,b,c,d+1,sum+p[x].v); 36 } 37 else if(p[x].k==2) 38 { 39 if(b<4) 40 dfs(x+1,a,b+1,c,d,sum+p[x].v); 41 } 42 else if(p[x].k==3) 43 { 44 if(c<2) 45 dfs(x+1,a,b,c+1,d,sum+p[x].v); 46 } 47 else if(a<4) 48 { 49 dfs(x+1,a+1,b,c,d,sum+p[x].v); 50 } 51 dfs(x+1,a,b,c,d,sum); 52 } 53 int main() 54 { 55 int i,n,v; 56 char s1[35],s2[35]; 57 while(cin>>p[1].s>>p[1].v>>p[1].po) 58 { 59 f.clear(); 60 f[p[1].s] = 1; 61 maxz=-INF;flag=0; 62 memset(w,0,sizeof(w)); 63 for(i = 2; i <=23 ;i++) 64 { 65 cin>>p[i].s>>p[i].v>>p[i].po; 66 } 67 for(i = 1; i <= 23 ; i++) 68 { 69 if(strcmp(p[i].po,"defender")==0) 70 p[i].k = 1; 71 else if(strcmp(p[i].po,"midfielder")==0) 72 p[i].k = 2; 73 else if(strcmp(p[i].po,"striker")==0) 74 p[i].k = 3; 75 else p[i].k = 4; 76 f[p[i].s] = i; 77 } 78 cin>>n; 79 for(i = 1; i <= n ; i++) 80 { 81 getchar(); 82 cin>>s1>>s2>>v; 83 w[f[s1]][f[s2]] = v; 84 w[f[s2]][f[s1]] = v; 85 } 86 dfs(1,0,0,0,0,0); 87 if(!flag) puts("impossible"); 88 else printf("%d\n",maxz); 89 } 90 return 0; 91 }