Arranging Your Team
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1529 Accepted Submission(s): 492
Problem Description
Your country has qualified for the FIFA 2010 South Africa World Cup. As the coach, you have made up the 23 men squad. Now you must select 11 of them as the starters. As is well known, there are four positions in soccer: goalkeeper, defender, midfielder and striker. Your favorite formation is 4-4-2, that is, you should choose 4 defenders, 4 midfielders, 2 strikers, and of course, 1 goalkeeper. As a retired ACMer, you want to write a program to help you make decision. Each person's ability has been evaluated as a positive integer. And what's more, for some special pairs of persons, if the two people are both on the field, there will be an additional effect (positive or negative). Now you should choose the 11 persons to make the total value maximum.
Input
There are multiple test cases, separated by an empty line. The first 23 lines of each test case indicate each person's name S
i, ability value V
i, and position. The length of each name is no more than 30, and there are no whitespaces in the names. All the names are different. The ability values are positive integers and no more than 100. The position is one of "goalkeeper", "defender", "midfielder" and "striker".
Then an integer M indicates that there are M special pairs. Each of the following M lines contains S i, S j and C ij, means that if S i and S j are both on the field, the additional profit is C ij. (-100 ≤ C ij ≤ 100). S i and S j are different strings, and must be in the previous 23 names. All the (S i, S j) pairs
are different.
Then an integer M indicates that there are M special pairs. Each of the following M lines contains S i, S j and C ij, means that if S i and S j are both on the field, the additional profit is C ij. (-100 ≤ C ij ≤ 100). S i and S j are different strings, and must be in the previous 23 names. All the (S i, S j) pairs
are different.
Output
Output one line for each test case, indicating the maximum total ability values, that is, the total ability values of the 11 persons plus the additional effects. If you cannot choose a 4-4-2 formation, output "impossible" instead.
Sample Input
Buffon 90 goalkeeper De_Sanctis 80 goalkeeper Marchetti 80 goalkeeper Zambrotta 90 defender Cannavaro 90 defender Chiellini 90 defender Maggio 90 defender Bonucci 80 defender Criscito 80 defender Bocchetti 80 defender Pirlo 90 midfielder Gattuso 90 midfielder De_Rossi 90 midfielder Montolivo 90 midfielder Camoranesi 80 midfielder Palombo 80 midfielder Marchisio 80 midfielder Pepe 80 midfielder Iaquinta 90 striker Di_Natale 90 striker Gilardino 80 striker Quagliarella 80 striker Pazzini 80 striker 1 Pirlo Quagliarella 50 ZhangSan01 50 goalkeeper ZhangSan02 50 defender ZhangSan03 50 defender ZhangSan04 50 defender ZhangSan05 50 defender ZhangSan06 50 defender ZhangSan07 50 defender ZhangSan08 50 defender ZhangSan09 50 defender ZhangSan10 50 defender ZhangSan11 50 defender ZhangSan12 50 defender ZhangSan13 50 defender ZhangSan14 50 defender ZhangSan15 50 defender ZhangSan16 50 midfielder ZhangSan17 50 midfielder ZhangSan18 50 midfielder ZhangSan19 50 midfielder ZhangSan20 50 midfielder ZhangSan21 50 midfielder ZhangSan22 50 midfielder ZhangSan23 50 midfielder 0
Sample Output
1030 impossible
Source
Recommend
这题就是XJB搜索啊,因为就23个人。而且是4-4-2-1的阵容。
所以复杂度最高也就23^4?嘻嘻。所以编号弄好,XJB搜索。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 1005
#define MAXN 100005
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
char name[100];
char type[100];
int power[30];
int maps[30][30];
map<string,int> mt;
map<string,int> na;
int mp[30];
vector<int> player[4];
vector<int> ans;
int ANS;
void dfs2(int pos,int sum);
void dfs3(int pos,int sum);
void dfs4(int pos,int sum);
void dfs1(int pos,int sum)
{
if(sum==1)
{
dfs2(-1,0);
return;
}
for(int i=pos+1; i<player[0].size(); i++)
{
ans.push_back(player[0][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<player[1].size(); i++)
{
ans.push_back(player[1][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<player[2].size(); i++)
{
ans.push_back(player[2][i]);
dfs3(i,sum+1);
ans.pop_back();
}
}
void dfs4(int pos,int sum)
{
if(sum==2)
{
int s=0;
for(int i=0; i<ans.size(); i++)
{
for(int j=i+1; j<ans.size(); j++)
{
s+=maps[ans[i]][ans[j]];
}
s+=power[ans[i]];
}
ANS=max(s,ANS);
return;
}
for(int i=pos+1; i<player[3].size(); i++)
{
ans.push_back(player[3][i]);
dfs4(i,sum+1);
ans.pop_back();
}
}
int main()
{
mt["goalkeeper"]=0;
mt["defender"]=1;
mt["midfielder"]=2;
mt["striker"]=3;
int p;
while(cin>>name)
{
ANS=-0x3f3f3f3f;
ans.clear();
na.clear();
for(int i=0; i<4; i++) player[i].clear();
cin>>p>>type;
player[mt[type]].push_back(1);
power[1]=p;
na[name]=1;
for(int i=2; i<=23; i++)
{
cin>>name>>p>>type;
na[name]=i;
player[mt[type]].push_back(i);
power[i]=p;
}
int n;
memset(maps,0,sizeof(maps));
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>name>>type>>p;
maps[na[name]][na[type]]=p;
maps[na[type]][na[name]]=p;
}
if(player[0].size()<1||player[1].size()<4||player[2].size()<4||player[3].size()<2)
{
cout<<"impossible"<<endl;
continue;
}
dfs1(-1,0);
cout<<ANS<<endl;
}
return 0;
}