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
**/
相關文章
- 2014上海網路賽1004||hdu5045 contest【狀態壓縮dp】
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- 演算法學習之路|狀態壓縮dp演算法
- NOIP2005過河[DP 狀態壓縮]
- 2014廣州網路賽1002||hdu5023 線段樹&&狀態壓縮
- URAL 1152 False Mirrors(簡單的狀態壓縮dp)False
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- 動態規劃——用二進位制表示集合的狀態壓縮DP動態規劃
- UVA 11825 dp、狀態壓縮、二進位制法表示集合
- 狀壓 dp
- 狀壓DP
- hdu3001 狀態壓縮dp+三進位制
- KM演算法——二分圖的最佳匹配演算法
- 論文閱讀 狀態壓縮
- POJ3279 Fliptile【狀態壓縮+DFS】
- 狀態壓縮動態規劃 -- 炮兵陣地動態規劃
- 2014廣州網路賽1004||hdu5025 分層最短路
- 2014鞍山網路賽 E題||hdu 5001 概率dp
- 二分圖匹配
- 常用的壓縮解壓縮以及網路通訊命令
- 一類哈密頓路徑/迴路為背景的狀壓dp
- DP最佳化——wqs二分
- HDU 5339 Untitled (狀態壓縮列舉)
- hdu2255 二分圖的最佳匹配 KM演算法演算法
- 動態規劃中初識狀態壓縮(入門)動態規劃
- hdu1074動態規劃狀態壓縮動態規劃
- 圖的匹配與網路流
- WQS 二分 & 凸最佳化dp
- 狀壓DP基礎入門
- POJ 3254 Corn Fields:網格密鋪類 狀壓dp
- 合理安排(狀壓dp,包括技巧)
- 深度神經網路的壓縮與加速神經網路
- 二分圖最大權完美匹配
- HDU 4022Bombing 2011網路賽(二分 或 STL)
- 二分圖的最大匹配、完美匹配和匈牙利演算法演算法
- E - Remove Pairs(狀壓dp+博弈論)REMAI
- POJ 2411 Mondriaan's Dream:網格密鋪類 狀壓dp
- 檢測網路狀態