您的位置:首页 > 理论基础 > 计算机网络

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

2017-03-06 22:12 260 查看

数字梯形问题

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>,<i.b>,建立附加源S汇T。

1、对于每个点i从<i.a>到<i.b>连接一条容量为1,费用为点i权值的有向边。

2、从S向梯形顶层每个<i.a>连一条容量为1,费用为0的有向边。

3、从梯形底层每个<i.b>向T连一条容量为1,费用为0的有向边。

4、对于每个点i和下面的两个点j,分别连一条从<i.b>到<j.a>容量为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
, nxt[M], tot;
int s, t;
int q
, inq
, d
, p
, a
;
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
[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
[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: