没什么好说的,最近太水,各种犯2...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;
const int inf = 1000000000;
struct Player {
string name;
int ability;
string position;
bool operator < (const Player &oth) const {
return position < oth.position;
}
} player[23];
int additional[23][23];
int find(const string &name) {
for (int i = 0; ; i++)
if (player[i].name == name) return i;
}
int pd, pg, pm, ps; // 分别是第一个后卫、第一个守门员、第一个中场、第一个前锋在player中的位置。
vector<int> select;
int ans;
void dfs(int x) {
if (select.size() >= 11) {
int tmp = 0;
for (int i = 0; i < 11; i++) {
tmp += player[select[i]].ability;
for (int j = i + 1; j < 11; j++)
tmp += additional[select[i]][select[j]];
}
if (tmp > ans) ans = tmp;
return;
}
if (x >= 23) return;
int y = x + 1;
if (select.size() >= 9) {
if (23 - y >= 11 - select.size()) dfs(y); // 如果可以不选x
select.push_back(x);
if (select.size() == 11) y = 23; // 如果选了x,已经有11人被选
dfs(y);
select.pop_back();
} else if (select.size() >= 5) {
if (ps - y >= 9 - select.size()) dfs(y); // 如果可以不选x
select.push_back(x);
if (select.size() == 9) y = ps; // 如果选了x,已经有9人被选(不需要中场了)
dfs(y);
select.pop_back();
} else if (select.size() >= 4) {
if (pm - y >= 5 - select.size()) dfs(y); // 如果可以不选x
select.push_back(x);
if (select.size() == 5) y = pm; // 如果选了x,已经有5人被选(不需要守门员了)
dfs(y);
select.pop_back();
} else {
if (pg - y >= 4 - select.size()) dfs(y); // 如果可以不选x
select.push_back(x);
if (select.size() == 4) y = pg; // 如果选了x,已经有4人被选(不需要后卫了)
dfs(y);
select.pop_back();
}
}
int main() {
int i, m, a, b, profit;
string namea, nameb;
while (cin >> player[0].name >> player[0].ability >> player[0].position) {
for (i = 1; i < 23; i++)
cin >> player[i].name >> player[i].ability >> player[i].position;
sort(player, player + 23);
cin >> m;
memset(additional, 0, sizeof(additional));
for (i = 0; i < m; i++) {
cin >> namea >> nameb >> profit;
a = find(namea);
b = find(nameb);
additional[a][b] =
additional[b][a] = profit;
}
pd = pg = pm = ps = -1;
for (i = 0; i < 23; i++) {
switch (player[i].position[0]) {
case 'd': if (pd == -1) pd = i; break;
case 'g': if (pg == -1) pg = i; break;
case 'm': if (pm == -1) pm = i; break;
case 's': if (ps == -1) ps = i; break;
}
}
if (pd == -1 || pg == -1 || pm == -1 || ps == -1 ||
pg - pd < 4 || pm - pg < 1 || ps - pm < 4 || 23 - ps < 2) {
cout << "impossible" << endl;
continue;
}
ans = -inf;
dfs(0);
cout << ans << endl;
}
return 0;
}