2014上海網路賽1004||hdu5045 二分圖的最佳匹配 或 狀態壓縮dp

life4711發表於2014-09-28

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
 

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
1 2 3 0.6 0.3 0.4 0.3 0.7 0.9
 

Sample Output
Case #1: 2.20000
這題資料只到10,學長是用狀態壓縮dp做的,自認為二分圖寫起來套費用流的模板時間更快一些。思路就是跑m/n+1次費用流,取和

#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
**/



相關文章