http://wikioi.com/problem/1913/

如果本题没有询问2和3,那么本题和蚯蚓那题一模一样。http://www.cnblogs.com/iwtwiioi/p/3935039.html

我们来分析询问2和3。

首先,询问2允许重复经过点。我们想想询问1的做法,是拆点,为什么?因为要控制只走一次。so,询问2就将拆的点去掉就行了。重构一次图,将上边的点向下连边,容量为1,费用为下边那个点的权值。然后现在还有第一行的点没有构造,那么将源连边到第一行的点,和之前相同,容量为1,费用为这些点的权值。接下来我们连最后一行到汇,因为可以允许重复点,那么就将容量设为oo,费用为0。(为什么这样不会走重复边呢?容量为1当然不会重复走。。)

然后我们来看询问3,只是在2的基础上加了个允许边重复。哈哈,重复边还不简单,将中间连的那些边容量全部设为oo,只不过在源连的边容量要为1,因为要限制次数。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=5500, M=2000000, oo=~0u>>1, s=0, t=5200, ss=t+1, tt=ss+1;
int ihead[N], cnt=1, d[N], p[N], n, m, vis[N], q[N], front, tail, id[35][70], nd[35][70];
struct ED { int from, to, cap, w, next; } e[M];
inline void add(const int &u, const int &v, const int &c, const int &w) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].from=u; e[cnt].cap=c; e[cnt].w=w;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].from=v; e[cnt].cap=0; e[cnt].w=-w;
}
inline const bool spfa(const int &s, const int &t) {
for1(i, 0, t) d[i]=1000000000, vis[i]=0;
vis[s]=1; d[s]=front=tail=0; q[tail++]=s;
int u, v, i;
while(front!=tail) {
u=q[front++]; if(front==N) front=0;
for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[v=e[i].to]>d[u]+e[i].w) {
d[v]=d[u]+e[i].w; p[v]=i;
if(!vis[v]) {
vis[v]=1, q[tail++]=v;
if(tail==N) tail=0;
}
}
vis[u]=0;
}
return d[t]!=1000000000;
}
int mcf(const int &s, const int &t) {
int ret=0, f, u;
while(spfa(s, t)) {
for(f=oo, u=t; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);
for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
ret+=d[t]*f;
}
return ret;
}
void rebuild() {
CC(ihead, 0); cnt=1; int now;
for1(i, 1, n) for1(j, 1, m+i-1) {
now=id[i][j];
if(i<n) add(now, id[i+1][j], 1, -nd[i+1][j]), add(now, id[i+1][j+1], 1, -nd[i+1][j+1]);
}
for1(j, 1, m) add(s, id[1][j], 1, -nd[1][j]);
for1(j, 1, m+n-1) add(id[n][j], t, oo, 0);
add(ss, s, oo, 0); add(t, tt, oo, 0);
}
int main() {
read(m); read(n);
int c, now, tot=0, pw=(n+m-1)*(n+m-1);
for1(i, 1, n) for1(j, 1, m+i-1) id[i][j]=++tot;
for1(i, 1, n) for1(j, 1, m+i-1) {
read(c); now=id[i][j]; nd[i][j]=c;
add(now, now+pw, 1, -c);
if(i<n) add(now+pw, id[i+1][j], 1, 0), add(now+pw, id[i+1][j+1], 1, 0);
}
for1(j, 1, m) add(s, id[1][j], 1, 0);
for1(j, 1, m+n-1) add(id[n][j]+pw, t, 1, 0);
add(ss, s, m, 0); add(t, tt, m, 0);
printf("%d\n", -mcf(ss, tt)); //case 2
rebuild();
printf("%d\n", -mcf(ss, tt)); //case 3
for(int i=2, tot=cnt; i<=tot; i+=2) if(e[i].from!=s) e[i].cap=oo, e[i^1].cap=0; else e[i].cap=1, e[i^1].cap=0;
printf("%d\n", -mcf(ss, tt)); return 0;
}

题目描述 Description

给定一个由n 行数字组成的数字梯形如下图所示。梯形的第一行有m 个数字。从梯形
的顶部的m 个数字开始,在每个数字处可以沿左下或右下方向移动,形成一条从梯形的顶
至底的路径。
规则1:从梯形的顶至底的m条路径互不相交。
规则2:从梯形的顶至底的m条路径仅在数字结点处相交。
规则3:从梯形的顶至底的m条路径允许在数字结点相交或边相交。

对于给定的数字梯形,分别按照规则1,规则2,和规则3 计算出从梯形的顶至底的m
条路径,使这m条路径经过的数字总和最大。

输入描述
Input Description

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

输出描述
Output Description

将按照规则1,规则2,和规则3 计算出的最大数字总和输出

样例输入
Sample Input

每行一个最大总和。

样例输出
Sample Output

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

数据范围及提示
Data Size & Hint

66
75
77

【wikioi】1913 数字梯形问题(费用流)的更多相关文章

  1. codevs 1913 数字梯形问题 费用流

    题目链接 给你一个数字梯形, 最上面一层m个数字, 然后m+1,......m+n-1个. n是层数. 在每个位置, 可以向左下或右下走.然后让你从最顶端的m个数字开始, 走出m条路径, 使得路过的数 ...

  2. 2018.10.15 loj#6010. 「网络流 24 题」数字梯形(费用流)

    传送门 费用流经典题. 按照题目要求建边. 为了方便我将所有格子拆点,三种情况下容量分别为111,infinfinf,infinfinf,费用都为validi,jval_{id_{i,j}}valid ...

  3. 洛谷P4013 数字梯形问题(费用流)

    题意 $N$行的矩阵,第一行有$M$个元素,第$i$行有$M + i - 1$个元素 问在三个规则下怎么取使得权值最大 Sol 我只会第一问qwq.. 因为有数量的限制,考虑拆点建图,把每个点拆为$a ...

  4. 【bzoj4514】: [Sdoi2016]数字配对 图论-费用流

    [bzoj4514]: [Sdoi2016]数字配对 好像正常的做法是建二分图? 我的是拆点然后 S->i cap=b[i] cost=0 i'->T cap=b[i] cost=0 然后 ...

  5. 【BZOJ4514】数字配对(费用流)

    题意: 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci× ...

  6. bzoj4514: [Sdoi2016]数字配对(费用流)

    传送门 ps:费用流增广的时候费用和流量打反了……调了一个多小时 每个数只能参与一次配对,那么这就是一个匹配嘛 我们先把每个数分解质因数,记质因子总个数为$cnt_i$,那如果$a_i/a_j$是质数 ...

  7. [SDOI2016]数字配对(费用流+贪心+trick)

    重点是如何找到可以配对的\(a[i]\)和\(a[j]\). 把\(a[i]\)分解质因数.设\(a[i]\)分解出的质因数的数量为\(cnt[i]\). 设\(a[i]\geq a[j]\) 那么\ ...

  8. BZOJ4514 [Sdoi2016]数字配对 【费用流】

    题目 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci×c ...

  9. wikioi 1035 火车停留 裸费用流

    链接:http://wikioi.com/problem/1035/ 怎么说呢,只能说这个建图很有意思.因为只有m条道,然后能互相接在一起的连通,对每个点进行拆点,很有意思的一道裸费用留题. 代码: ...

随机推荐

  1. Linux系统管理员面试50题

    命令nslookup是做什么的? Nslookup 是一个 监测网络中 DNS 服务器是否能正确实现域名解析的命令行工具. 你如何把CPU占用率最高的进程显示出来? top -c 按照cpu排序 如果 ...

  2. SpringMVC配置easyui-datagrid

    SprimgMVC的UserController.java @RequestMapping(value = "listUserForJson") @ResponseBody pub ...

  3. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  4. Java for LeetCode 045 Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. DisJSet:Wireless Network(POJ 2236)

      无线电网络 题目大意:就是地震后,所有的电脑都坏了,现在可以修复,而且要重新连成一个网络,两台电脑之间最大连接距离为D,两台电脑可以有中继电脑,按O修复电脑,按S测试两台电脑是否有链接,如果有就输 ...

  6. mybatis There is no getter for property named 'xx' in 'class java.lang.String

    转载自://http://www.cnblogs.com/anee/p/3324140.html 用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no getter ...

  7. JDK1.7 HashMap 源码分析

    概述 HashMap是Java里基本的存储Key.Value的一个数据类型,了解它的内部实现,可以帮我们编写出更高效的Java代码. 本文主要分析JDK1.7中HashMap实现,JDK1.8中的Ha ...

  8. BaseColumns以及自定义Column

    android provider 包下自带的BaseColumn /* * Copyright (C) 2006 The Android Open Source Project * * License ...

  9. linux下文件压缩与解压操作

    对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可 ...

  10. Android之智能问答机器人

    本文主要利用图灵机器人的接口,所做的一个简单的智能问答机器人 实现 由于发送与接收消息都是不同的listView,所以要用有两个listVeiw的布局文件 接收消息布局文件 <?xml vers ...