三倍经验,三个条件,分别对应了常见的3种模型,第一种是限制每个点只能一次且无交点,我们可以把这个点拆成一个出点一个入点,capacity为1,这样就限制了只选择一次,第二种是可以有交点,但不能有交边,那我们就不需要拆点,限制每条capacity都为1就可以了,第三种直接连,没有限制(

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; const int maxm = 1e6+;
const int INF = 0x3f3f3f3f; struct edge{
int u, v, cap, flow, cost, nex;
} edges[maxm]; int head[maxm], cur[maxm], cnt, fa[maxm], d[maxm], buf[][], num[][], ID;
bool inq[maxm]; void init() {
memset(head, -, sizeof(head));
} void add(int u, int v, int cap, int cost) {
edges[cnt] = edge{u, v, cap, , cost, head[u]};
head[u] = cnt++;
} void addedge(int u, int v, int cap, int cost) {
add(u, v, cap, cost), add(v, u, , -cost);
} bool spfa(int s, int t, int &flow, LL &cost) {
//for(int i = 0; i <= n+1; ++i) d[i] = INF; //init()
memset(d, , sizeof(d));
memset(inq, false, sizeof(inq));
d[s] = , inq[s] = true;
fa[s] = -, cur[s] = INF;
queue<int> q;
q.push(s);
while(!q.empty()) {
int u = q.front();
q.pop();
inq[u] = false;
for(int i = head[u]; i != -; i = edges[i].nex) {
edge& now = edges[i];
int v = now.v;
if(now.cap > now.flow && d[v] > d[u] + now.cost) {
d[v] = d[u] + now.cost;
fa[v] = i;
cur[v] = min(cur[u], now.cap - now.flow);
if(!inq[v]) {q.push(v); inq[v] = true;}
}
}
}
if(d[t] == INF) return false;
flow += cur[t];
cost += 1LL*d[t]*cur[t];
for(int u = t; u != s; u = edges[fa[u]].u) {
edges[fa[u]].flow += cur[t];
edges[fa[u]^].flow -= cur[t];
}
return true;
} int MincostMaxflow(int s, int t, LL &cost) {
cost = ;
int flow = ;
while(spfa(s, t, flow, cost));
return flow;
} void run_case() {
int m, n; cin >> m >> n;
int s = , t = 1e6;
// first
init();
for(int i = m; i < n+m; ++i)
for(int j = ; j <= i; ++j) {
num[i][j] = ++ID;
cin >> buf[i][j];
addedge(ID<<, (ID<<)|, , -buf[i][j]);
}
for(int i = m; i < n+m-; ++i) {
for(int j = ; j <= i; ++j) {
addedge((num[i][j]<<)|, num[i+][j]<<, , );
addedge((num[i][j]<<)|, num[i+][j+]<<, , );
}
}
for(int i = ; i <= m; ++i) addedge(s, num[m][i]<<, , );
for(int i = ; i < n+m; ++i) addedge((num[n+m-][i]<<)|, t, , );
LL cost = ;
MincostMaxflow(s, t, cost);
cout << -cost << "\n";
// second
init();
for(int i = m; i < n+m-; ++i)
for(int j = ; j <= i; ++j) {
addedge(num[i][j], num[i+][j], , -buf[i+][j]);
addedge(num[i][j], num[i+][j+], , -buf[i+][j+]);
}
for(int i = ; i <= m; ++i) addedge(s, num[m][i], , -buf[m][i]);
for(int i = ; i < n+m; ++i) addedge(num[n+m-][i], t, INF, );
cost = , MincostMaxflow(s, t, cost);
cout << -cost << "\n";
// third
init();
for(int i = m; i < n+m-; ++i)
for(int j = ; j <= i; ++j) {
addedge(num[i][j], num[i+][j], INF, -buf[i+][j]);
addedge(num[i][j], num[i+][j+], INF, -buf[i+][j+]);
}
for(int i = ; i <= m; ++i) addedge(s, num[m][i], , -buf[m][i]);
for(int i = ; i < n+m; ++i) addedge(num[n+m-][i], t, INF, );
cost = , MincostMaxflow(s, t, cost);
cout << -cost << "\n";
} int main() {
ios::sync_with_stdio(false), cin.tie();
run_case();
cout.flush();
return ;
}

luogu P4013 数字梯形问题的更多相关文章

  1. P4013 数字梯形问题 网络流二十四题

    P4013 数字梯形问题 题目描述 给定一个由 nn 行数字组成的数字梯形如下图所示. 梯形的第一行有 m 个数字.从梯形的顶部的 m 个数字开始,在每个数字处可以沿左下或右下方向移动,形成一条从梯形 ...

  2. P4013 数字梯形问题 网络流

    题目描述 给定一个由 nn 行数字组成的数字梯形如下图所示. 梯形的第一行有 mm 个数字.从梯形的顶部的 mm 个数字开始,在每个数字处可以沿左下或右下方向移动,形成一条从梯形的顶至底的路径. 分别 ...

  3. P4013 数字梯形问题

    \(\color{#0066ff}{题目描述}\) 给定一个由 \(n\) 行数字组成的数字梯形如下图所示. 梯形的第一行有 \(m\) 个数字.从梯形的顶部的 \(m\) 个数字开始,在每个数字处可 ...

  4. 洛谷P4013数字梯形问题——网络流24题

    题目:https://www.luogu.org/problemnew/show/P4013 最大费用最大流裸题: 注意:在第二种情况中,底层所有点连向汇点的边容量应该为inf,因为可以有多条路径结束 ...

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

    传送门 两个感受:码量感人……大佬nb…… 规则一:$m$条路径都不相交,那么每一个点只能经过一次,那么考虑拆点,把每一个点拆成$A_{i,j}$和$B_{i,j}$,然后两点之间连一条容量$1$,费 ...

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

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

  7. 洛谷 P4013 数字梯形问题【最大费用最大流】

    第一问:因为每个点只能经过一次,所以拆点限制流量,建(i,i',1,val[i]),然后s向第一行建(s,i,1,0),表示每个点只能出发一次,然后最后一行连向汇点(i',t,1,0),跑最大费用最大 ...

  8. 洛谷 P4013 数字梯形问题

    ->题目链接 题解: 网络流. #include<cstdio> #include<iostream> #include<queue> #include< ...

  9. 【费用流】【网络流24题】【P4013】 数字梯形问题

    Description 给定一个由 \(n\) 行数字组成的数字梯形如下图所示. 梯形的第一行有 \(m\) 个数字.从梯形的顶部的 \(m\) 个数字开始,在每个数字处可以沿左下或右下方向移动,形成 ...

随机推荐

  1. linux下windows访问samba服务器

    今天学习windows如何访问linux的共享文件. 开通samba服务,首先需要在linux下安装samba程序包,又再次使用光盘镜像挂载: [root@localhost home]# mount ...

  2. T114048 [RC-02] yltx数对 (打表)

    这题如果全部打表的话,文件大小会有65kb,超了,所以只打出一半,前一半用程序算就可以了,并不会超时. 如果算法优化的好,其实可以打的更少. #include <bits/stdc++.h> ...

  3. qq音乐解析API

    文档:www.tjit.net 开放的接口:api88.net 个人代码: input2(event){ //将字符转化为encodeURL编码,才能进行正确请求,这是这个接口要求的 //js自带的转 ...

  4. vue axios路由跳转取消所有请求 和 防止重复请求

    直接上干货 在发送第二次请求的时候如果第一次请求还未返回,则取消第一次请求,以保证后发送的请求返回的数据不会被先发送的请求覆盖. 或者是跳转路由的时候取消还未返回的请求 第一步: axios 怎么取消 ...

  5. VMWare tools

    一.首先是安装VMWare tools1.以ROOT身份进入LINUX2.在虚拟机软件VMWARE状态栏中,点击 SETTING菜单下的ENABLE VMWARE TOOLS子菜单,此时在linux的 ...

  6. @media screen 自适应笔记

    在css中使用@media screen 通过检索宽度 对应改变html中class的css属性. 1280分辨率以上(大于1200px) @media screen and (min-width:1 ...

  7. linux chrome rpm chrome浏览器下载(ver 63-70)

    我的github chrome下载地址:https://github.com/chen1932390299/python 国内开源的资源 chrome下载centos 的:https://www.ch ...

  8. JS-判断null值

    单独判断 null var str = null; if(str === null){ alert("is null"); } 同时判断 null 和 undefined 虽然nu ...

  9. Python中Numpy.nonzero()函数

    Numpy.nonzero()返回的是数组中,非零元素的位置.如果是二维数组就是描述非零元素在几行几列,三维数组则是描述非零元素在第几组中的第几行第几列. 举例如下: 二维数组: a = np.arr ...

  10. 【原】mac电脑使用总结

    mac下终端配置(item2+oh-my-zsh)+solarized配色方案:https://www.cnblogs.com/weixuqin/p/7029177.html