Province Region Competition Team Play | ||||||
| ||||||
Description | ||||||
As we know, ACM competition is not only based on personal talents, but also team works. A team can be outstanding once it combines these two factors. A school has N ACM contest candidates. The coach wants to select K candidates from N candidates and sends them to Hunan Province Region Competition. We suppose every team has 3 members and every member has a value A that represents the personal skills. Every pair of members has a value W that shows the teamwork skills for that pair. There are 3 members a, b, c in a team then the integral skills of this team represents as following formula: A[a]+A[b]+A[c]+W[a][b]+W[a][c]+W[b][c]; In the rules of Province Region Competition, team score is very important. A coach hope to set K teams up and the total team score is maximum. Can you figure out what the maximum score of K teams if reasonably choosing team members from contest candidates? | ||||||
Input | ||||||
The first line has an integer T (T <=10) represents the number of cases. For each test cases, the first line has two numbers K, N (1 <= K <= 6, 3*K<=N<=18) which show the number of teams and the number of candidates. The second line has N integers A1.. An ,(0<=Ai<=100000) which represents the personal talent or personal skills for each candidates. The following N lines, every line has N integers which is a matrix Wnn. Wij describe the teamwork skill between team member i and j, 0<=Wij<=100000 ,and Wij=Wji. | ||||||
Output | ||||||
For every case, output an integer which is maximum score for K teams. | ||||||
Sample Input | ||||||
1
| ||||||
Sample Output | ||||||
66
| ||||||
Source | ||||||
Hunan University 2011 the 7th Programming Contest | ||||||
Recommend | ||||||
万祥 |
题目大意:
给你N个人,让你组成K个队伍出征,每个队伍有三个人,每个队伍的能力值为:a【A】+a【B】+a【C】+W【A】【B】+W【A】【C】+W【B】【C】;
问你能够组成的方案中,能量的最大值。
思路:
1、观察到数据范围不大,又因为一种中间选取方案会影响继续选择组队方式的结果,那么肯定要状压dp的.
暴力思路很简单,设定dp【i】表示为已经组完了队伍的状态下的最大值。
那么就有dp【i】=dp【q】+a【A】+a【B】+a【C】+W【A】【B】+W【A】【C】+W【B】【C】;这里A.B.C需要三层for来枚举
那么很显然,我们枚举完状态再枚举三层for的时间复杂度为:O(n^3*(1<<n)),大概估计了一下,是有1e8的.
十组数据3s,肯定跑不过去。
抱着试一试的心态,还是跑了一下,果然答案是TLE的....
2、那么考虑思维优化。
①这里我们引入一个分段处理思维,首先我们可以三层for预处理暴力枚举出来组队的方案,方案数就是C(n,3),最大也就800+种选取方案,记为chose【i】。
同时处理出val【i】表示第i种方案组队的价值.
②那么此时我们考虑,如果k==1.那么显然用枚举出来的方案直接求一个最大值即可。
如果k==2.我们可以将两个组队方法拼接在一起,此时我们可以两层for枚举两种组队方式,如果可以进行拼接(没有重复选取一个人的情况),那么对应得到一个组两个队的一个方案,同时能够维护这个方案得到的最大值。
那么我们可以将这种思路拓展K次.
而且第一次拓展需要枚举C(n,3)*C(n,3)次,然而第二次只需要枚举C(n,6)*C(n,3).依次是减少的,那么拓展K次,显然时间复杂度是降下来的.
总时间复杂度:O(C(n,3)*C(n,3)+C(n,6)*C(n,3)+C(n,9)*C(n,3).................................);
③那么我们对于枚举出来的chose【i】,直接设定为dp【chose【i】】=val【i】,作为选取第一个队伍结束后的dp值.
那么可以维护一个队列,其中存上一次得到的状态(假设当前是第1次拓展,那么存的就是chose【i】(共C(n,3)个),拓展得到组完两个队的情况.)(假设当前是第2次拓展,那么对应存的就是组完两次的情况(共C(n,6)个),拓展得到组完三个队的情况)拓展得到的继续进行记录.
过程维护:dp【v】=dp【s.front】+val【i】(v=s.front+chose【i】)这里需要chose【i】和s.front没有重复出现的队员;
3、描述起来可能稍微有些乱,大家不妨看代码更加清晰。
总体来说这个题也是非常不错的一个题,想了很久.
Ac代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
vector<int >mp[25];
int w[50][50];
int a[50];
int dp[(1<<20)];
int vis[(1<<20)];
int chose[1000];
int val[1000];
int n,k,cnt;
void init()
{
memset(chose,0,sizeof(chose));
cnt=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(i==j||j==k||i==k)continue;
chose[cnt]+=(1<<i);
chose[cnt]+=(1<<j);
chose[cnt]+=(1<<k);
val[cnt]=a[i]+a[j]+a[k]+w[i][j]+w[i][k]+w[j][k];
cnt++;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&k,&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&w[i][j]);
}
}
init();
int end=(1<<n);
queue<int >s;
memset(dp,0,sizeof(dp));
for(int i=0;i<cnt;i++)s.push(chose[i]),dp[chose[i]]=val[i];
for(int i=0;i<k-1;i++)
{
memset(vis,0,sizeof(vis));
while(!s.empty())
{
int u=s.front();
s.pop();
for(int j=0;j<cnt;j++)
{
if((u&chose[j])==0)
{
int v=u+chose[j];
dp[v]=max(dp[v],dp[u]+val[j]);
if(vis[v]==0)
{
vis[v]=1;
}
}
}
}
for(int j=0;j<end;j++)
{
if(vis[j]==1)
{
s.push(j);
}
}
}
int output=0;
for(int i=0;i<end;i++)
{
int num=0;
for(int j=0;j<n;j++)
{
if((i&(1<<j))==0)continue;
else num++;
}
if(num==k*3)
output=max(output,dp[i]);
}
printf("%d\n",output);
}
}