网络流24题16. 数字梯形问题

数字梯形问题

Description

给定一个由 n 行数字组成的数字梯形如下图所示。梯形的第一行有 m 个数字。从梯形的顶部的 m 个数字开始,在每个数字处可以沿左下或右下方向移动,形成一条从梯形的顶至底的路径。
规则 1:从梯形的顶至底的 m条路径互不相交。
规则 2:从梯形的顶至底的 m条路径仅在数字结点处相交。
规则 3:从梯形的顶至底的 m条路径允许在数字结点相交或边相交。
这里写图片描述
对于给定的数字梯形,分别按照规则 1,规则 2,和规则 3 计算出从梯形的顶至底的 m 条路径,使这 m条路径经过的数字总和最大。

Input

第 1 行中有 2 个正整数 m和 n(m,n<=20),分别表示数字梯形的第一行有 m 个数字,共有 n 行。接下来的 n 行是数字梯形中各行的数字。第 1 行有 m个数字,第 2 行有 m+1 个数字,…。

Output

输出按照规则 1,规则 2,和规则 3 计算出的最大数字总和。
每行一个最大总和。

Sample Input

2 5
2 3
3 4 5
9 10 9 1
1 1 10 1 1
1 1 10 12 1 1

Sample Output

66
75
77

题解

规则(1)
把梯形中每个位置抽象为两个点 <i.a> <script type="math/tex" id="MathJax-Element-57"> </script>, <i.b> <script type="math/tex" id="MathJax-Element-58"> </script>,建立附加源S汇T。
1、对于每个点i从 <i.a> <script type="math/tex" id="MathJax-Element-59"> </script>到 <i.b> <script type="math/tex" id="MathJax-Element-60"> </script>连接一条容量为1,费用为点i权值的有向边。
2、从S向梯形顶层每个 <i.a> <script type="math/tex" id="MathJax-Element-61"> </script>连一条容量为1,费用为0的有向边。
3、从梯形底层每个 <i.b> <script type="math/tex" id="MathJax-Element-62"> </script>向T连一条容量为1,费用为0的有向边。
4、对于每个点i和下面的两个点j,分别连一条从 <i.b> <script type="math/tex" id="MathJax-Element-63"> </script>到 <j.a> <script type="math/tex" id="MathJax-Element-64"> </script>容量为1,费用为0的有向边。
求最大费用最大流,费用流值就是结果。

规则(2)
把梯形中每个位置看做一个点i,建立附加源S汇T。
1、从S向梯形顶层每个i连一条容量为1,费用为0的有向边。
2、从梯形底层每个i向T连一条容量为无穷大,费用为i的权值的有向边。
3、对于每个点i和下面的两个点j,分别连一条从i到j容量为1,费用为点i权值的有向边。
求最大费用最大流,费用流值就是结果。

规则(3)
把梯形中每个位置看做一个点i,建立附加源S汇T。
1、从S向梯形顶层每个i连一条容量为1,费用为0的有向边。
2、从梯形底层每个i向T连一条容量为无穷大,费用为i的权值的有向边。
3、对于每个点i和下面的两个点j,分别连一条从i到j容量为无穷大,费用为点i权值的有向边。
求最大费用最大流,费用流值就是结果。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 1000 * 2 + 10, M = 1000000 + 10, inf = 0x3f3f3f3f;

struct Edge{
    int fr, to, cap, flow, cost;
}edg[M];
int hd[N], nxt[M], tot;
int s, t;
int q[N], inq[N], d[N], p[N], a[N];
int n, m, mp[50][50];
void insert(int u, int v, int w, int x){
    edg[tot].fr = u, edg[tot].to = v, edg[tot].cap = w, edg[tot].flow = 0, edg[tot].cost = x;
    nxt[tot] = hd[u]; hd[u] = tot;
    tot++;
    edg[tot].fr = v, edg[tot].to = u, edg[tot].cap = 0, edg[tot].flow = 0, edg[tot].cost = -x;
    nxt[tot] = hd[v]; hd[v] = tot;
    tot++;
}
bool spfa(int &fl, int &cst){
    for(int i = s; i <= t; i++) d[i] = -inf;
    int head = 0, tail = 1;
    q[0] = s; inq[s] = 1;
    d[s] = 0; p[s] = 0; a[s] = inf;
    while(head != tail){
        int u = q[head++]; if(head == 2001) head = 0;
        inq[u] = 0;
        for(int i = hd[u]; i >= 0; i = nxt[i]){
            Edge &e = edg[i];
            if(e.cap > e.flow && d[e.to] < d[u] + e.cost){
                d[e.to] = d[u] + e.cost;
                p[e.to] = i;
                a[e.to] = min(a[u], e.cap - e.flow);
                if(!inq[e.to]){
                    q[tail++] = e.to; if(tail == 2001) tail = 0;
                    inq[e.to] = 1;
                }
            }
        }
    }
    if(d[t] == -inf) return false;
    fl += a[t];
    cst += a[t] * d[t];
    int u = t;
    while(u != s){
        edg[p[u]].flow += a[t];
        edg[p[u]^1].flow -= a[t];
        u = edg[p[u]].fr;
    }
    return true;
}
int maxflow(){
    int flow = 0, cost = 0;
    while(spfa(flow, cost));
    return cost;
}
void init(){
    scanf("%d%d", &m, &n);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m + i - 1; j++)
            scanf("%d", &mp[i][j]);
}
int get(int i, int j){
    return (m + m + i - 2) * (i - 1) / 2 + j;
}
void work1(){
    int num = get(n, n + m - 1);
    s = 0, t = num * 2 + 1;
    memset(hd, -1, sizeof(hd));
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m + i - 1; j++){
        insert(get(i, j), num + get(i, j), 1, mp[i][j]);
        if(i < n){
            insert(num + get(i, j), get(i+1, j), 1, 0);
            insert(num + get(i, j), get(i+1, j+1), 1, 0);
        }
    }
    for(int i = 1; i <= m; i++)
        insert(s, i, 1, 0);
    for(int i = 1; i <= n + m -1; i++)
        insert(num + get(n, i), t, 1, 0);
    printf("%d\n", maxflow());
}
void work2(){
    memset(hd, -1, sizeof(hd));
    memset(nxt, 0, sizeof(nxt));
    tot = 0;
    for(int i = 1; i <= m; i++)
        insert(s, i, 1, 0);
    for(int i = 1; i <= n + m - 1; i++)
        insert(get(n, i), t, inf, mp[n][i]);
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m + i - 1; j++)
        if(i < n){
            insert(get(i, j), get(i+1, j), 1, mp[i][j]);
            insert(get(i, j), get(i+1, j+1), 1, mp[i][j]);
        }
    printf("%d\n", maxflow());
}
void work3(){
    memset(hd, -1, sizeof(hd));
    memset(nxt, 0, sizeof(nxt));
    tot = 0;
    for(int i = 1; i <= m; i++)
        insert(s, i, 1, 0);
    for(int i = 1; i <= n + m - 1; i++)
        insert(get(n, i), t, inf, mp[n][i]);
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m + i - 1; j++)
        if(i < n){
            insert(get(i, j), get(i+1, j), inf, mp[i][j]);
            insert(get(i, j), get(i+1, j+1), inf, mp[i][j]);
        }
    printf("%d\n", maxflow());
}
int main(){
    freopen("prog816.in", "r", stdin);
    freopen("prog816.out", "w", stdout);
    init();
    work1();
    work2();
    work3();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值