Shanghai Regional Online Contest 1004
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题的状态,然后取最大的值。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<vector> 7 #define M(a,b) memset(a,b,sizeof(a)) 8 9 using namespace std; 10 11 12 int t; 13 double num[16][1006]; 14 double dp[1006][1<<10]; 15 int n,m; 16 double max(double a,double b) 17 { 18 return a>b?a:b; 19 } 20 21 int main() 22 { 23 scanf("%d",&t); 24 int cas = 1; 25 while(t--) 26 { 27 scanf("%d%d",&n,&m); 28 for(int i = 0;i<n;i++) 29 for(int j = 0;j<m;j++) 30 { 31 scanf("%lf",&num[i][j]); 32 } 33 M(dp,0); 34 for(int i = 0;i<n;i++) 35 dp[0][1<<i] = num[i][0]; 36 for(int i = 1;i<m;i++) 37 { 38 for(int j = 0;j<(1<<n);j++) 39 { 40 if(dp[i-1][j]) 41 { 42 43 for(int k = 0;k<n;k++) 44 { 45 if((j&(1<<k))==0) 46 { 47 int tm = j|(1<<k); 48 if(tm==((1<<n)-1)) tm = 0; 49 dp[i][tm] = max(dp[i][tm],dp[i-1][j]+num[k][i]); 50 //cout<<i<<' '<<tm<<' '<<dp[i][tm]<<' '<<dp[i-1][j]+num[k][i]<<endl; 51 } 52 } 53 } 54 } 55 } 56 double ans = -1; 57 for(int i = 0;i<(1<<n);i++) 58 { 59 if(dp[m-1][i]>ans) ans = dp[m-1][i]; 60 } 61 printf("Case #%d: ",cas++); 62 printf("%.5f\n",ans); 63 } 64 return 0; 65 }
这一题还可以用费用流或KM搞,很简单,做m/n次KM就行。
1 //O(n^3*(m/n)) 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 #include<vector> 8 #define M(a,b) memset(a,b,sizeof(a)) 9 #define INF 0x3f3f3f3f 10 using namespace std; 11 12 const double eps=1e-9; 13 int n,m; 14 double num[16][1006]; 15 int match[1006]; 16 double slack[1006]; 17 bool visx[1006]; 18 bool visy[1006]; 19 double lx[1006]; 20 double ly[1006]; 21 double g[1006][1006]; 22 int n1,n2; 23 24 bool dfs(int x) 25 { 26 visx[x] = 1; 27 for(int y = 1;y<=n2;y++) 28 { 29 if(visy[y]) 30 continue; 31 double t = lx[x]+ly[y]-g[x][y]; 32 if(fabs(t)<eps) 33 { 34 visy[y] = 1; 35 if(match[y]==-1||dfs(match[y])) 36 { 37 match[y] = x; 38 return true; 39 } 40 } 41 else if(slack[y]>t) 42 slack[y] = t; 43 } 44 return false; 45 } 46 47 double KM() 48 { 49 M(ly,0); 50 M(match,-1); 51 int i,j; 52 for(i = 1;i<=n1;i++) 53 { 54 for(j = 1,lx[i]=-INF;j<=n2;j++) 55 { 56 if(g[i][j]>lx[i]) 57 lx[i] = g[i][j]; 58 } 59 } 60 for(int k = 1;k<=n1;k++) 61 { 62 for(j = 0;j<=n2;j++) 63 slack[j] = INF; 64 while(true) 65 { 66 M(visx,false); 67 M(visy,false); 68 if(dfs(k)) break; 69 double t = INF; 70 for(i = 1;i<=n2;i++) 71 if(!visy[i]&&t>slack[i]) 72 t = slack[i]; 73 for(i = 1;i<=n1;i++) 74 if(visx[i]) 75 lx[i]-=t; 76 for(i = 1;i<=n2;i++) 77 if(visy[i]) 78 ly[i]+=t; 79 else 80 slack[i]-=t; 81 //cout<<t<<endl; 82 } 83 //cout<<k<<endl; 84 } 85 double ans = 0; 86 for(i = 1;i<=n2;i++) 87 { 88 if(match[i]!=-1) 89 ans+=g[match[i]][i];//cout<<g[match[i]][i]<<'?'<<endl;} 90 } 91 //cout<<ans<<'!'<<endl; 92 return ans; 93 } 94 95 int main() 96 { 97 int t; 98 scanf("%d",&t); 99 int cas = 1; 100 while(t--) 101 { 102 M(num,0); 103 scanf("%d%d",&n,&m); 104 for(int i = 0;i<n;i++) 105 for(int j = 0;j<m;j++) 106 { 107 scanf("%lf",&num[i][j]); 108 } 109 double ans = 0; 110 n1 = n2 = n; 111 for(int i = 0;i<m;i+=n) 112 { 113 for(int j = 0;j<n;j++) 114 { 115 for(int k = i;k<i+n;k++) 116 { 117 g[j+1][k-i+1] = num[j][k]; 118 } 119 } 120 ans+=KM(); 121 //cout<<ans<<endl; 122 } 123 printf("Case #%d: %.5f\n",cas++,ans); 124 } 125 return 0 ; 126 }

浙公网安备 33010602011771号