2014上海網路賽1004||hdu5045 二分圖的最佳匹配 或 狀態壓縮dp
http://acm.hdu.edu.cn/showproblem.php?pid=5045
Problem Description
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
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 .
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
1
2 3
0.6 0.3 0.4
0.3 0.7 0.9
Sample Output
Case #1: 2.20000
#include<cstdio>
#include<iostream>
#include <string.h>
using namespace std;
const int oo=1e9;
const int mm=11111;
const int mn=888;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
double cost[mm],dis[mn];
int head[mn],p[mn],q[mn],vis[mn];
void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0; i<node; ++i)head[i]=-1,vis[i]=0;
edge=0;
}
void addedge(int u,int v,int f,double c)
{
ver[edge]=v,flow[edge]=f,cost[edge]=c,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,cost[edge]=-c,next[edge]=head[v],head[v]=edge++;
}
bool spfa()
{
int i,u,v,l,r=0;
double tmp;
for(i=0; i<node; ++i)
dis[i]=oo;
dis[q[r++]=src]=0;
p[src]=p[dest]=-1;
for(l=0; l!=r; (++l>=mn)?l=0:l)
for(i=head[u=q[l]],vis[u]=0; i>=0; i=next[i])
if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i]))
{
dis[v]=tmp;
p[v]=i^1;
if(vis[v])continue;
vis[q[r++]=v]=1;
if(r>=mn)r=0;
}
return p[dest]>-1;
}
double SpfaFlow()
{
int i,delta;
double ret=0;
while(spfa())
{
for(i=p[dest],delta=oo; i>=0; i=p[ver[i]])
if(flow[i^1]<delta)delta=flow[i^1];
for(i=p[dest]; i>=0; i=p[ver[i]])
flow[i]+=delta,flow[i^1]-=delta;
ret+=delta*dis[dest];
}
return ret;
}
int n,m,T;
double num[15][1005];
int main()
{
int tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%lf",&num[i][j]);
double sum=0;
for(int k=0;k<=m/n;k++)
{
prepare(n*2+2,0,n*2+1);
for(int i=1;i<=n;i++)
{
addedge(src,i,1,0);
addedge(i+n,dest,1,0);
for(int j=1;j<=n;j++)
addedge(i,j+n,1,-num[i][j+k*n]);
}
sum+=(-SpfaFlow());
}
printf("Case #%d: %.5lf\n",++tt,sum);
}
return 0;
}
DP+狀態壓縮。dp[i][j]表示前i道題目j個人答題狀態的最大值,j用二進位制表示,因為人最多就10個。因為每兩個人之間答題數目不能超過1,所以當狀態達到1 << n - 1,即所有人都答過一題時,將重置為0。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int m,n;
double a[15][1250],dp[1205][1250];
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%lf",&a[i][j]);
for(int i=0;i<=m;i++)
for(int j=0;j<1<<n;j++)
dp[i][j]=-1.0;
dp[0][0]=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<(1<<n);j++)
{
if(dp[i][j]<0)
continue;
for(int k=0;k<n;k++)
{
if(!((1<<k)&j))
{
int st=j|(1<<k);
if(st==(1<<n)-1)
st=0;
dp[i+1][st]=max(dp[i+1][st],dp[i][j]+a[k][i]);
}
}
}
}
/*for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
printf("%lf ",dp[i][j]);
printf("\n");
}*/
double ans=0;
for(int i=0;i<(1<<n);i++)
ans=max(ans,dp[m][i]);
printf("Case #%d: %.5lf\n",++tt,ans);
}
return 0;
}
/**
99
3 4
0.4 0.8 0.1 0.1
0.3 0.7 0.1 0.1
0.5 0.6 0.1 0.1
3 4
0.3 0.6 0.1 0.1
0.5 0.8 0.1 0.1
0.4 0.7 0.1 0.1
2 3
0.6 0.3 0.4
0.3 0.7 0.9
ANSWER:1.5,1.4,2.2
**/
相關文章
- bzoj4145: [AMPPZ2014]The Prices(狀態壓縮+Dp)
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- 簡易狀態壓縮DP
- HDU 5816 Hearthstone(狀態壓縮DP+概率)
- hdu--5418Victor and World+狀態壓縮DP
- [狀壓dp] 最短Hamilton路徑(模板題+狀壓dp)
- 狀態壓縮
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- 動態規劃——用二進位制表示集合的狀態壓縮DP動態規劃
- 狀壓 dp
- 狀壓DP
- HDU 3006 The Number of set (狀態壓縮)
- 論文閱讀 狀態壓縮
- POJ3279 Fliptile【狀態壓縮+DFS】
- 一類哈密頓路徑/迴路為背景的狀壓dp
- 二分圖匹配
- DP最佳化——wqs二分
- 動態規劃中初識狀態壓縮(入門)動態規劃
- 圖的匹配與網路流
- POJ 2777 Count Color (線段樹+狀態壓縮)
- HDU 1074 Doing Homework(狀壓DP)
- 狀壓DP基礎入門
- WQS 二分 & 凸最佳化dp
- 深度神經網路的壓縮與加速神經網路
- 合理安排(狀壓dp,包括技巧)
- 最佳化部落格Ⅰ-壓縮圖片為webp格式Web
- POJ - 3041 Asteroids 【二分圖匹配】AST
- 二分圖最大權完美匹配
- 狀壓 + 網路流 -- Escape HDU - 3605
- NOI2001 炮兵陣地(狀壓dp)
- E - Remove Pairs(狀壓dp+博弈論)REMAI
- 前端圖片壓縮 - H5&Uni-App圖片壓縮前端H5APP
- 影像體積壓縮工具JPEG Jackal更好的壓縮圖片
- 二分圖最小點覆蓋等於二分圖最大匹配
- (最大流,二分圖的多重匹配) Magic Potion
- 匈牙利演算法--二分圖的最大匹配演算法
- 大模型原理:遞迴、壓縮和模式匹配大模型遞迴模式
- ??圖片壓縮CanvasCanvas
- canvas 壓縮圖片Canvas