HDU 5045 Contest(状压DP或费用流)

93 篇文章 0 订阅
48 篇文章 0 订阅

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5045

 

题意:N个人去做M道题,给出这N个人解这M道题的概率,且任意两人做题数不能相差1,问做出所有题的最大期望

 

思路:因为做题数不超过1的限制,这相当于是以N个人为一轮做N道题,做完M道为止,故而可以用dp[i][j]代表解决前i道题时状态为j,当j == (1 << n) - 1时清零。

同样因为N个人解N道问题,这就变成了二分图最大权匹配问题,跑(M / N) + 1次费用流也行

 

 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <string>
#include <bitset>
#include <ctime>

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

const int maxn = 10010;
const int inf = 0x3f3f3f3f;

double p[20][1010], dp[1010][(1 << 10) + 20];

int main()
{
    int t;
    cin >> t;
    for (int ca = 1; ca <= t; ca++)
    {
        printf("Case #%d: ", ca);

        int n, m;
        scanf("%d%d", &n, &m);

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                scanf("%lf", &p[i][j]);

        for (int i = 0; i <= m; i++)
            for (int j = 0; j <= (1 << n); j++)
                dp[i][j] = -1;

        dp[0][0] = 0;
        for (int j = 1; j <= m; j++)
            for (int i = 0; i < (1 << n); i++)
            {
                if (dp[j - 1][i] == -1) continue;

                for (int k = 1; k <= n; k++)
                {
                    int s =  (1 << (k - 1));
                    if (i & s) continue;

                    s |= i;
                    if (s == (1 << n) - 1) s = 0;
                    dp[j][s] = max(dp[j][s], dp[j - 1][i] + p[k][j]);
                }
            }

        double ans = 0;
        for (int i = 0; i < (1 << n); i++)
            ans = max(ans, dp[m][i]);
        printf("%.5f\n", ans);
    }
    return 0;
}

 

 

 

 

 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

const int MAXN = 100;
const int MAXM = 50000;
const int INF = 0x3f3f3f3f;

typedef long long ll;

struct Edge
{
    int to, next, cap, flow;
    double cost;
} edge[MAXM];

int head[MAXN], tol;
int pre[MAXN];
double dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1

void init(int n)
{
    N = n;
    tol = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int cap, double cost)
{
    edge[tol].to = v;
    edge[tol].cap = cap;
    edge[tol].cost = cost;
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].cost = -cost;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}

bool spfa(int s, int t)
{
    queue<int>q;
    for (int i = 0; i < N; i++)
    {
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if (edge[i].cap > edge[i].flow &&
                    dis[v] > dis[u] + edge[i].cost )
            {
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i;
                if (!vis[v])
                {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if (pre[t] == -1) return false;
    else return true;
}

//返回的是最大流,cost存的是最小费用
double minCostMaxflow(int s, int t, double &cost)
{
    int flow = 0;
    cost = 0;
    while (spfa(s, t))
    {
        int Min = INF;
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
        {
            if (Min > edge[i].cap - edge[i].flow)
                Min = edge[i].cap - edge[i].flow;
        }
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
        {
            edge[i].flow += Min;
            edge[i ^ 1].flow -= Min;
            cost += edge[i].cost * Min;
        }
        flow += Min;
    }
    return flow;
}

double p[20][1010];

int main()
{
    int t;
    cin >> t;
    for (int ca = 1; ca <= t; ca++)
    {
        printf("Case #%d: ", ca);

        int n, m;
        scanf("%d%d", &n, &m);

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                scanf("%lf", &p[i][j]);

        double ans = 0, res;
        int s = 0, t = n * 2 + 1;
        for (int k = 0; k < m / n; k++)
        {
            init(t + 1);
            for (int i = 1; i <= n; i++)
            {
                addedge(s, i, 1, 0);
                addedge(i + n, t, 1, 0);

                for (int j = n + 1; j <= n * 2; j++)
                {
                    addedge(i, j, 1, -p[i][(j - n) + k * n]);
                }
            }

            minCostMaxflow(s, t, res);
            ans += res;
        }

        init(t + 1);
        for (int i = n + 1; i <= n + (m % n); i++)
            addedge(i, t, 1, 0);
        for (int i = 1; i <= n; i++)
        {
            addedge(s, i, 1, 0);

            for (int j = n + 1; j <= n + (m % n); j++)
            {
                addedge(i, j, 1, -p[i][(j - n) + n * (m / n)]);
            }
        }

        minCostMaxflow(s, t, res);
        ans += res;
        printf("%.5f\n", -ans);
    }
    return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值