Prelude

传送到Codeforces:0.0


Solution

板子题,在这里贴个板子。

这题面是smg?题面中有说每个点只能经过一次吗?是我瞎了吗?

因为这WA on test 27一个小时,烦死了,浪费时间。


Code

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cassert> using namespace std;
const int MAXN = 1000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
int _w; int read() {
int x;
_w = scanf( "%d", &x );
return x;
} namespace MCMF {
struct Edge {
int u, v, c, f, w;
Edge() {}
Edge( int u, int v, int c, int f, int w ):
u(u), v(v), c(c), f(f), w(w) {}
}; int n, m, s, t;
int head[MAXN], nxt[MAXM];
Edge edge[MAXM]; void init( int _n ) {
n = _n, m = 0;
for( int i = 0; i < n; ++i )
head[i] = -1;
}
void adde( int u, int v, int c, int w ) {
edge[m] = Edge(u, v, c, 0, w);
nxt[m] = head[u], head[u] = m++;
edge[m] = Edge(v, u, 0, 0, -w);
nxt[m] = head[v], head[v] = m++;
} queue<int> q;
int dis[MAXN], inq[MAXN], res[MAXN], from[MAXN]; bool spfa() {
for( int i = 0; i < n; ++i )
dis[i] = INF, inq[i] = 0;
dis[s] = 0, inq[s] = 1, q.push(s), res[s] = INF;
while( !q.empty() ) {
int u = q.front(); q.pop();
inq[u] = 0;
for( int i = head[u]; ~i; i = nxt[i] ) {
const Edge &e = edge[i];
if( e.c > e.f && dis[u] + e.w < dis[e.v] ) {
dis[e.v] = dis[u] + e.w;
from[e.v] = i;
res[e.v] = min( res[u], e.c-e.f );
if( !inq[e.v] )
inq[e.v] = 1, q.push(e.v);
}
}
}
return dis[t] != INF;
}
void augment() {
int f = res[t], u = t;
while( u != s ) {
int i = from[u];
edge[i].f += f;
edge[i^1].f -= f;
u = edge[i].u;
}
}
int solve( int _s, int _t ) {
s = _s, t = _t;
int cost = 0;
while( spfa() ) {
cost += res[t] * dis[t];
augment();
}
return cost;
}
} int n, k, g[MAXN][MAXN];
int s, t, ss, tt, nid;
int in[MAXN], out[MAXN]; void adde( int u, int v, int l, int r, int w ) {
MCMF::adde(ss, v, l, 0);
MCMF::adde(u, tt, l, 0);
MCMF::adde(u, v, r-l, w);
} void solve() {
s = nid++, t = nid++, ss = nid++, tt = nid++;
for( int i = 1; i <= n; ++i )
in[i] = nid++, out[i] = nid++;
MCMF::init(nid);
adde(t, s, 0, INF, 0);
adde(s, out[1], 0, k, 0);
for( int i = 2; i <= n; ++i ) {
adde(out[i], t, 0, INF, 0);
adde(in[i], out[i], 1, 1, 0); // 每个点只能经过一次
}
for( int i = 1; i <= n; ++i )
for( int j = i+1; j <= n; ++j )
adde(out[i], in[j], 0, INF, g[i][j]);
printf( "%d\n", MCMF::solve(ss, tt) );
} int main() {
n = read()+1, k = read();
for( int i = 1; i <= n; ++i )
for( int j = i+1; j <= n; ++j )
g[i][j] = read();
solve();
return 0;
}

【题解】Catering World Finals 2015 上下界费用流的更多相关文章

  1. 【BZOJ3876】[Ahoi2014]支线剧情 有上下界费用流

    [BZOJ3876][Ahoi2014]支线剧情 Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩 ...

  2. 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...

  3. 【BZOJ2055】80人环游世界 有上下界费用流

    [BZOJ2055]80人环游世界 Description     想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么     一个 ...

  4. 【bzoj1927】[Sdoi2010]星际竞速 有上下界费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832464.html 题目描述 10年一度的银河系赛车大赛又要开始了.作为全银河最盛大的活动之一,夺得这个项目的冠军 ...

  5. 【有源汇上下界费用流】BZOJ 3876 [Ahoi2014]支线剧情

    题目链接: http://www.lydsy.com:808/JudgeOnline/problem.php?id=3876 题目大意: 给定一张拓扑图(有向无环图),每条边有边权,每次只能从第一个点 ...

  6. BZOJ 3876: [Ahoi2014]支线剧情 [上下界费用流]

    3876: [Ahoi2014]支线剧情 题意:每次只能从1开始,每条边至少经过一次,有边权,求最小花费 裸上下界费用流...每条边下界为1就行了 注意要加上下界*边权 #include <io ...

  7. BZOJ.1927.[SDOI2010]星际竞速(无源汇上下界费用流SPFA /最小路径覆盖)

    题目链接 上下界费用流: /* 每个点i恰好(最少+最多)经过一次->拆点(最多)+限制流量下界(i,i',[1,1],0)(最少) 然后无源汇可行流 不需要源汇. 注: SS只会连i',求SS ...

  8. BZOJ2324 ZJOI2011营救皮卡丘(floyd+上下界费用流)

    虽然不一定每次都是由编号小的点向编号大的走,但一个人摧毁的顺序一定是从编号小的到编号大的.那么在摧毁据点x的过程中,其只能经过编号小于x的点.并且这样一定合法,因为可以控制其他人先去摧毁所经过的点.那 ...

  9. 【Luogu】P1251餐巾计划(上下界费用流)

    题目链接 学了一下上下界费用流,似乎很nb.但是我说得不好,所以这里给出博客链接. 某dalao的博客 然后这道题的解法就是先用上下界费用流的建图方式连早上和晚上之间的那条边,保证当天一定会有r条或以 ...

随机推荐

  1. python之爬虫_模块

    目录 一.requests模块二.Beautifulsoup模块 一.requests模块 1.介绍 Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但 ...

  2. django_models后台管理myarya

    arya重点代码 # urls.py from django.urls import path,re_path,include from arya.service import v1 urlpatte ...

  3. RN 离线包集成后需要注意的一些问题

    1.ReactNative 开发中如何去掉iOS状态栏的"Loading from..." 等淡黑色的弹框,很难看? 在 AppDelegate.h 中引入: #import &l ...

  4. 《TCP/IP 详解 卷1:协议》第 5 章:Internet 协议

    IP 是 TCPIP 协议族中的核心协议.所有 TCP.UDP.ICMP.IGMP 数据都通过 IP 数据包(又称为 packet)来传输.IP 的英文名为 Internet Protocol,是互联 ...

  5. C++编译与链接(2)-浅谈内部链接与外部链接

    发现每次写技术博客时,都会在文章开头处花费一番功夫 ...从前,有一个程序员....他的名字叫magicsoar 为什么有时会出现aaa已在bbb中重定义的错误? 为什么有时会出现无法解析的外部符号? ...

  6. Android自定义View实现仿QQ实现运动步数效果

    效果图: 1.attrs.xml中 <declare-styleable name="QQStepView"> <attr name="outerCol ...

  7. 在dell服务器上装windows server 2012详细解析

    壹: 首先确定磁盘阵列的问题,在dell服务器开机后按住 Ctrl+R 或者 F2 会展开虚拟磁盘创建菜单 详细步骤可以查看:https://jingyan.baidu.com/article/915 ...

  8. Python入门:类与类的继承

    类,是一些有共同特征和行为事物的抽象概念的总和. 1. 定义一个类: 我们使用class来定义一个类,和之前说过的定义函数用def类似.在类里面给变量赋值时,专业术语称之为类的属性. 比如拿可口可乐来 ...

  9. mysqldumpslow 分析slow query日志和explain分析mysql查询结构

    mysqldumpslow的使用:比如我们要查询按时间返回前5条日志信息,格式如下:mysqldumpslow -s t -t 5 /var/log/mysql/slowquery_20180303. ...

  10. mysql my.cnf 或my.ini配置文件参数解释(转):

    #*** client options 相关选项 ***# #以下选项会被MySQL客户端应用读取.注意只有MySQL附带的客户端应用程序保证可以读取这段内容.如果你想你自己的MySQL应用程序获取这 ...