【bzoj3876】 Ahoi2014—支线剧情
http://www.lydsy.com/JudgeOnline/problem.php?id=3876 (题目链接)
题意
给出一张拓扑图,每条边有一个权值,问每次从1号点出发,走遍所有的边所需要的最小花费是多少。
Solution
上下界最小费用可行流。
因为每条边至少要被经过一次,所以每条边有个流量下界1。又因为每个节点都可以作为结束点,那么每个节点都有可能成为汇点,所以每个点都要向源点1连一条容量为无穷费用为0的边。
这样套上上下界网络流的模型,求解费用最小的可行流即可。
细节
边数$n^2$
代码
// bzoj3876
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf (1ll<<30)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std; const int maxn=1010;
int cnt=1,n,head[maxn],S,T,ans;
int vis[maxn],dis[maxn],f[maxn],p[maxn]; struct edge {int from,to,next,w,c;}e[maxn*maxn]; void link(int u,int v,int w,int c) {
e[++cnt]=(edge){u,v,head[u],w,c};head[u]=cnt;
e[++cnt]=(edge){v,u,head[v],0,-c};head[v]=cnt;
}
bool SPFA() {
for (int i=S;i<=T;i++) dis[i]=inf;
queue<int> q;q.push(S);dis[S]=0;f[S]=inf;
while (!q.empty()) {
int x=q.front();q.pop();
vis[x]=0;
for (int i=head[x];i;i=e[i].next) if (e[i].w && dis[e[i].to]>dis[x]+e[i].c) {
dis[e[i].to]=dis[x]+e[i].c;
f[e[i].to]=min(f[x],e[i].w);
p[e[i].to]=i;
if (!vis[e[i].to]) q.push(e[i].to),vis[e[i].to]=1;
}
}
if (dis[T]==inf) return 0;
for (int i=p[T];i;i=p[e[i].from]) e[i].w-=f[T],e[i^1].w+=f[T];
ans+=dis[T]*f[T];
return 1;
}
int main() {
scanf("%d",&n);
S=0,T=n+1;
for (int k,i=1;i<=n;i++) {
scanf("%d",&k);
for (int x,y,j=1;j<=k;j++) {
scanf("%d%d",&x,&y);
link(i,x,inf,y);
link(S,x,1,y);
}
link(i,T,k,0);
if (i!=1) link(i,1,inf,0);
}
while (SPFA());
printf("%d",ans);
return 0;
}
【bzoj3876】 Ahoi2014—支线剧情的更多相关文章
- bzoj3876: [Ahoi2014]支线剧情
神犇题解:http://blog.csdn.net/popoqqq/article/details/43024221 题意:给定一个DAG,1为起始点,任意一个点可以直接回到1,每条边有经过代价,求一 ...
- [bzoj3876][AHOI2014]支线剧情——上下界费用流
题目 传送门 题解 建立s和t,然后s向1连下限0上限inf费用0的边,除1外所有节点向t连下限0上限inf费用0的边,对于每条边下限为1上限为inf费用为经过费用,然后我们只有做上下界网络流构出新图 ...
- 【BZOJ3876】[Ahoi2014]支线剧情 有上下界费用流
[BZOJ3876][Ahoi2014]支线剧情 Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩 ...
- 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 821 Solved: 502[Submit][Status ...
- C++之路进阶——bzoj3876(支线剧情)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...
- bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 484 Solved: 296[Submit][Status ...
- BZOJ 3876: [Ahoi2014]支线剧情 [上下界费用流]
3876: [Ahoi2014]支线剧情 题意:每次只能从1开始,每条边至少经过一次,有边权,求最小花费 裸上下界费用流...每条边下界为1就行了 注意要加上下界*边权 #include <io ...
- BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流
3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...
- [Ahoi2014]支线剧情[无源汇有下界最小费用可行流]
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1538 Solved: 940[Submit][Statu ...
- 【BZOJ3876】 [Ahoi2014]支线剧情
Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往 都有很多的支线剧情,现 ...
随机推荐
- PHP实现验证码制作
captcha.php(PHP产生验证码并储存Session): <?php //开启Session session_start(); //绘制底图 $image = imagecreatetr ...
- [UWP 自定义控件]了解模板化控件(9):UI指南
1. 使用TemplateSettings统一外观 TemplateSettings提供一组只读属性,用于在新建ControlTemplate时使用这些约定的属性. 譬如,修改HeaderedCont ...
- Jmeter(二十八)_Docker+Jmeter+Gitlab+Jenkins+Ant(容器化的接口自动化持续集成平台)
这套接口自动化持续集成环境已经部署差不多了,现在说说我的设计思路 1:利用Docker容器化Gitlab,Jenkins,Jmeter,Ant,链接如下 Docker_容器化gitlab Docker ...
- 安装zkpython出错
pip3 install zkpython==0.4.2 提示:zookeeper.c:20:23: 致命错误:zookeeper.h:没有那个文件或目录 解决: 1.是否安装python-devel ...
- hadoop-mapreduce-(1)-统计单词数量
编写map程序 package com.cvicse.ump.hadoop.mapreduce.map; import java.io.IOException; import org.apache.h ...
- #科委外文文献发现系统——导出word模板1.0
ps:该篇文档由实验室ljg提供. Crowdsourcing 一. 技术简介 Crowdsourcing, a modern business term coined in ...
- javac编译提示错误需要为 class、interface 或 enum
HelloWorld.java:1: 需要为 class.interface 或 enum锘缝ublic class HelloWorld{^1 错误 这个错误出现的原因主要是在中文操作系统中,使用一 ...
- 自己实现数据结构系列二---LinkedList
一.先上代码: 1.方式一: public class LinkedList<E> { //节点,用来存放数据:数据+下一个元素的引用 private class Node{ privat ...
- lumen或者laravel安装指定版本
方法一 安装器安装:缺点不能安装指定版本 composer global require "laravel/lumen-installer" lumen new blog comp ...
- Centos7 yum安装Chrome浏览器
一.创建yum源文件 cd /etc/yum.repo.d/ touch google-chrome.repo 二.输入yum源信息 [google-chrome] name=google-chrom ...