题目链接:支线剧情

  这道题就是一道裸裸的上下界网络流……只不过这道题边带了权,那么建出图之后跑费用流即可。

  首先需要新建超级源\(S\)和超级汇\(T\)。对于这道题,对于一条边\((u,v,z)\),我们从\(S\)向\(v\)连一条容量为\(1\),费用为\(z\)的边,保证下界;从\(u\)向\(T\)连一条容量为\(1\),费用为\(0\)的边,也是用来保证下界的。由于没有上界,所以直接从\(u\)向\(v\)连一条容量为\(INF\),费用为\(z\)的边即可。

  注意这道题可以在任意一个点重新开始,所以等于除\(1\)号点之外所有的点都可能成为汇点,所以连边的时候需要注意一下。也就是说,除了\(1\)号点之外的所有点,都要向\(1\)连一条容量为\(INF\),费用为\(0\)的边来保证流量平衡。

  下面贴代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define maxn 310
#define maxm 50010
#define INF (1<<29) using namespace std;
typedef long long llg; int n,S,T,c[maxm],f[maxm],ans;
int head[maxn],next[maxm],to[maxm],tt=1;
int d[maxn],dep[maxn],fr[maxn];
bool w[maxn]; int getint(){
int w=0;bool q=0;
char c=getchar();
while((c>'9'||c<'0')&&c!='-') c=getchar();
if(c=='-') c=getchar(),q=1;
while(c>='0'&&c<='9') w=w*10+c-'0',c=getchar();
return q?-w:w;
} void link(int x,int y,int z,int o){
to[++tt]=y;next[tt]=head[x];head[x]=tt;
to[++tt]=x;next[tt]=head[y];head[y]=tt;
c[tt-1]=z; f[tt-1]=o; f[tt]=-o;
} bool spfa(){
for(int i=1;i<=T;i++) dep[i]=INF;
int ld=0,rd=0; dep[d[rd++]=S]=0;
while(ld!=rd){
int u=d[ld++]; w[u]=0; if(ld==maxn) ld=0;
for(int i=head[u],v;v=to[i],i;i=next[i])
if(c[i] && dep[v]>dep[u]+f[i]){
dep[v]=dep[u]+f[i]; fr[v]=i;
if(!w[v]){
w[v]=1;
if(dep[v]<=dep[d[ld]]) ld--,ld+=maxn,ld%=maxn,d[ld]=v;
else d[rd++]=v,rd%=maxn;
}
}
}
return dep[T]!=INF;
} int get(){
int now=INF,ans=0;
for(int u=T;u!=S;u=to[fr[u]^1]) now=min(now,c[fr[u]]),ans+=f[fr[u]];
for(int u=T;u!=S;u=to[fr[u]^1]) c[fr[u]]-=now,c[fr[u]^1]+=now;
return now*ans;
} int main(){
File("a");
n=getint(); S=n+1,T=n+2;
for(int i=1,x,u,z;i<=n;i++){
x=getint();
if(i!=1) link(i,1,INF,0);
if(x) link(i,T,x,0);
while(x--){
u=getint(); z=getint();
link(i,u,INF,z),link(S,u,1,z);
}
}
while(spfa()) ans+=get();
printf("%d",ans);
return 0;
}

BZOJ 3876 【AHOI2014】 支线剧情的更多相关文章

  1. bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 484  Solved: 296[Submit][Status ...

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

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

  3. BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流

    3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...

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

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

  5. bzoj 3876: [Ahoi2014]支线剧情

    就是加一个1的下界就好了. #include<bits/stdc++.h> #define N 100005 #define LL long long #define inf 0x3f3f ...

  6. BZOJ 3876:支线剧情(有下界最小费用最大流)

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

  7. [Ahoi2014]支线剧情[无源汇有下界最小费用可行流]

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1538  Solved: 940[Submit][Statu ...

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

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

  9. 【BZOJ3876】 [Ahoi2014]支线剧情

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

  10. bzoj 3876: [Ahoi2014&Jsoi2014]支线剧情【有上下界有源汇最小费用最大流】

    每条边流量有下界有费用,很显然是有上下界有源汇最小费用最大流 连边(s,1,(0,inf),0),(i,t,(0,inf),0),表示从1出发inf次从每个点结束inf次 连边(i,j,(1,inf) ...

随机推荐

  1. C# DateTime.Now详细用法

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  2. .net配置404错误页面

    如果你的网站出现一堆让人看不懂的报错,那么你就不是一个合格的程序员,也不是一个合格的站长. 下面的方面可以帮助你的网站远离让人头大的页面. 第一步:配置web.config 打开web.config, ...

  3. 强制关机导致ORA-03113

    数据库启动报错:无法打开数据库. [oracle@localhost ORCL]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Productio ...

  4. LINEAR HASH Partitioning

    MySQL :: MySQL 8.0 Reference Manual :: 23.2.4.1 LINEAR HASH Partitioning https://dev.mysql.com/doc/r ...

  5. DNS named. bind linux (ACL/View)---dnsmasq-with docker,hosts in docker.

    [bind--named.conf] https://blog.csdn.net/z_yttt/article/details/53020814 [Docker搭建dnsmasq] https://b ...

  6. Python开发【模块】:torndb

    Torndb模块 概要:torndb是一个轻量级的基于MySQLdb封装的一个模块,其是tornado框架的一部分.其项目主页为:https://github.com/bdarnell/torndb ...

  7. Mac OS X 在 Finder 里快速创建文本文件

    Automator 新建一个 Application   选取:应用程序 实用工具→运行AppleScript 添加一个动作 "Run AppleScript"代码如下: on r ...

  8. HDU1575:Tr A(矩阵快速幂模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1575   #include <iostream> #include <string.h> ...

  9. 【Lua】模块与包

    定义:         从用户观点来看,一个模块就是一个程序库,可以通过require来加载(require用于使用模块,module用于创建模块),然后便得到了一个全局变量,表示一个table,这个 ...

  10. python 面向对象编程学习总结

    面向对象是个抽象的东西,概念比较多,下面会一一介绍. 一.类和实例 类(Class)和实例(Instance)是面向对象最重要的概念. 类是指抽象出的模板.实例则是根据类创建出来的具体的“对象”,每个 ...