Shanghai Regional Online Contest 1004

In the ACM International Collegiate Programming Contest, each team consist of three students. And the teams are given 5 hours to solve between 8 and 12 programming problems.

On Mars, there is programming contest, too. Each team consist of N students. The teams are given M hours to solve M programming problems. Each team can use only one computer, but they can’t cooperate to solve a problem. At the beginning of the ith hour, they will get the ith programming problem. They must choose a student to solve this problem and others go out to have a rest. The chosen student will spend an hour time to program this problem. At the end of this hour, he must submit his program. This program is then run on test data and can’t modify any more.

Now, you have to help a team to find a strategy to maximize the expected number of correctly solved problems.

For each problem, each student has a certain probability that correct solve. If the ith student solve the jth problem, the probability of correct solve is Pij .

At any time, the different between any two students’ programming time is not more than 1 hour. For example, if there are 3 students and there are 5 problems. The strategy {1,2,3,1,2}, {1,3,2,2,3} or {2,1,3,3,1} are all legal. But {1,1,3,2,3},{3,1,3,1,2} and {1,2,3,1,1} are all illegal.

You should find a strategy to maximize the expected number of correctly solved problems, if you have know all probability

Input

The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,M (1 ≤ N ≤ 10,1 ≤ M ≤ 1000),denoting the number of students and programming problem, respectively.

The next N lines, each lines contains M real numbers between 0 and 1 , the jth number in the ith line is Pij .

Output

For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then a single real number means the maximal expected number of correctly solved problems if this team follow the best strategy, to five digits after the decimal point. Look at the output for sample input for details.

Sample Input

12 30.6 0.3 0.40.3 0.7 0.9

状压dp,dp[i][j]表示解到第i道题时状态是j的最大期望值,j表示n个人与他们最小解题数的差值,容易知道只有二进制位是0的时候可以转移,二进制全1即为0。

当一个状态已经存在时(最优解),则根据他去更新下一个状态,也就是,当更新第i道题的状态时,去找i-1题的状态,然后取最大的值。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define M(a,b) memset(a,b,sizeof(a)) using namespace std; int t;
double num[][];
double dp[][<<];
int n,m;
double max(double a,double b)
{
return a>b?a:b;
} int main()
{
scanf("%d",&t);
int cas = ;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i = ;i<n;i++)
for(int j = ;j<m;j++)
{
scanf("%lf",&num[i][j]);
}
M(dp,);
for(int i = ;i<n;i++)
dp[][<<i] = num[i][];
for(int i = ;i<m;i++)
{
for(int j = ;j<(<<n);j++)
{
if(dp[i-][j])
{ for(int k = ;k<n;k++)
{
if((j&(<<k))==)
{
int tm = j|(<<k);
if(tm==((<<n)-)) tm = ;
dp[i][tm] = max(dp[i][tm],dp[i-][j]+num[k][i]);
//cout<<i<<' '<<tm<<' '<<dp[i][tm]<<' '<<dp[i-1][j]+num[k][i]<<endl;
}
}
}
}
}
double ans = -;
for(int i = ;i<(<<n);i++)
{
if(dp[m-][i]>ans) ans = dp[m-][i];
}
printf("Case #%d: ",cas++);
printf("%.5f\n",ans);
}
return ;
}

这一题还可以用费用流或KM搞,很简单,做m/n次KM就行。

 //O(n^3*(m/n))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std; const double eps=1e-;
int n,m;
double num[][];
int match[];
double slack[];
bool visx[];
bool visy[];
double lx[];
double ly[];
double g[][];
int n1,n2; bool dfs(int x)
{
visx[x] = ;
for(int y = ;y<=n2;y++)
{
if(visy[y])
continue;
double t = lx[x]+ly[y]-g[x][y];
if(fabs(t)<eps)
{
visy[y] = ;
if(match[y]==-||dfs(match[y]))
{
match[y] = x;
return true;
}
}
else if(slack[y]>t)
slack[y] = t;
}
return false;
} double KM()
{
M(ly,);
M(match,-);
int i,j;
for(i = ;i<=n1;i++)
{
for(j = ,lx[i]=-INF;j<=n2;j++)
{
if(g[i][j]>lx[i])
lx[i] = g[i][j];
}
}
for(int k = ;k<=n1;k++)
{
for(j = ;j<=n2;j++)
slack[j] = INF;
while(true)
{
M(visx,false);
M(visy,false);
if(dfs(k)) break;
double t = INF;
for(i = ;i<=n2;i++)
if(!visy[i]&&t>slack[i])
t = slack[i];
for(i = ;i<=n1;i++)
if(visx[i])
lx[i]-=t;
for(i = ;i<=n2;i++)
if(visy[i])
ly[i]+=t;
else
slack[i]-=t;
//cout<<t<<endl;
}
//cout<<k<<endl;
}
double ans = ;
for(i = ;i<=n2;i++)
{
if(match[i]!=-)
ans+=g[match[i]][i];//cout<<g[match[i]][i]<<'?'<<endl;}
}
//cout<<ans<<'!'<<endl;
return ans;
} int main()
{
int t;
scanf("%d",&t);
int cas = ;
while(t--)
{
M(num,);
scanf("%d%d",&n,&m);
for(int i = ;i<n;i++)
for(int j = ;j<m;j++)
{
scanf("%lf",&num[i][j]);
}
double ans = ;
n1 = n2 = n;
for(int i = ;i<m;i+=n)
{
for(int j = ;j<n;j++)
{
for(int k = i;k<i+n;k++)
{
g[j+][k-i+] = num[j][k];
}
}
ans+=KM();
//cout<<ans<<endl;
}
printf("Case #%d: %.5f\n",cas++,ans);
}
return ;
}

最新文章

  1. Zookeeper的安装和使用
  2. Nhibernate随手记(1)
  3. 分布式缓存Memcached---开篇的话
  4. ifconfig 下面的一些字段(errors, dropped, overruns)
  5. velocity分页模板
  6. postgresql 配置文件优化
  7. win32 应用程序 添加资源
  8. Unity3D开发赛车Demo遇到的问题
  9. HUE的时区问题
  10. linux下对普通用户设置文件访问控命令之setfacl
  11. Hibernate笔记——hql总结
  12. hdu1792 水题
  13. HDU 5045(Contest-费用流)[template:费用流]
  14. 推送GitHub报错 fatal: Out of memory, malloc failed 解决办法
  15. jenkins使用2----基本实例
  16. (网页)javascript该如何学习?怎么样才能学好?
  17. 洗礼灵魂,修炼python(50)--爬虫篇—基础认识
  18. javaweb下载中的一个问题
  19. jqgrid 点击列头的超链接或按钮时,不触发列排序事件
  20. Linux常用rmp包网址

热门文章

  1. 关于 android 开发中 debug不能顺利进行的各种问题的总结
  2. C++ Pitfalls 之 reference to an object in a dynamically allocated containter
  3. HDU 2544 单源最短路
  4. 【转】python编码规范
  5. angular评论星级指令
  6. java8 中的时间和数据的变化
  7. windows7-PowerDesigner 15.1 的安装图解
  8. 非阻塞socket学习,select基本用法
  9. k-nearest-neighbor算法
  10. CentOS只允许部分IP登陆ssh |ssh 允许指定IP