标签:
DES:从23个队员中选出4—4—2—1共4种11人来组成比赛队伍。给出每个人对每个职位的能力值。给出m组人在一起时会产生的附加效果。问你整场比赛人员的能力和最高是多少。
用深搜暴力枚举每种类型的人选择情况。感觉是这个深搜写的很机智。
在vector中存结构体就会很慢。TLE。直接存序号就AC了。以后还是尽量少用结构体吧。
#include<stdio.h> #include<string.h> #include<map> #include<vector> #include<iostream> using namespace std; vector<int>play[4]; vector<int>ans; int pow[30]; map<string, int>mtype; map<string, int>mname; int eff[40][40]; int maxn = 0; void dfs2(int, int ); void dfs3(int, int ); void dfs4(int, int ); void dfs1(int pos, int sum) { if (sum == 1) { dfs2(-1, 0); return; } for (int i=pos+1; i<play[1].size(); ++i) { ans.push_back(play[1][i]); dfs1(i, sum+1); ans.pop_back(); } } void dfs2(int pos, int sum) { if (sum == 4) { dfs3(-1, 0); return; } for (int i=pos+1; i<play[2].size(); ++i) { ans.push_back(play[2][i]); dfs2(i, sum+1); ans.pop_back(); } } void dfs3(int pos, int sum) { if (sum == 4) { dfs4(-1, 0); return; } for (int i=pos+1; i<play[3].size(); ++i) { ans.push_back(play[3][i]); dfs3(i, sum+1); ans.pop_back(); } } void dfs4(int pos, int sum) { if (sum == 2) { int anss = 0; for (int i=0; i<ans.size(); ++i) { for (int j=i+1; j<ans.size(); ++j) { anss += eff[ans[i]][ans[j]]; } anss += pow[ans[i]]; } if (anss > maxn) maxn = anss; return; } for (int i=pos+1; i<play[0].size(); ++i) { ans.push_back(play[0][i]); dfs4(i, sum+1); ans.pop_back(); } } int main() { mtype["goalkeeper"] = 1; mtype["defender"] = 2; mtype["midfielder"] = 3; mtype["striker"] = 0; char name[40], type[30]; int power; while(cin >> name) { maxn = -0x3f3f3f3f; ans.clear(); for (int i=0; i<4; ++i) play[i].clear(); cin >> power >> type; int tt = mtype[type]; pow[0] = power; mname[name] = 0; play[tt].push_back(0); memset(eff, 0, sizeof(eff)); for (int i=1; i<=22; ++i) { cin >> name >> power >> type; pow[i] = power; mname[name] = i; int tt = mtype[type]; play[tt].push_back(i); } int n; cin >> n; int n1, n2, add; char na1[40], na2[40]; for (int i=1; i<=n; ++i) { cin >> na1 >> na2 >> add; n1 = mname[na1]; n2 = mname[na2]; eff[n1][n2] = add; eff[n2][n1] = add; } int a[4] = {2, 1, 4, 4}; bool flag = true; for (int i=0; i<4; ++i) { if (play[i].size() < a[i]) { flag = false; break; } } if (!flag) { cout << "impossible\n"; continue; } dfs1(-1, 0); cout << maxn << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/icode-girl/p/4704162.html