[bzoj3876][AHOI2014]支线剧情——上下界费用流
题目
题解
建立s和t,然后s向1连下限0上限inf费用0的边,除1外所有节点向t连下限0上限inf费用0的边,对于每条边下限为1上限为inf费用为经过费用,然后我们只有做上下界网络流构出新图,跑最小费用可行流即可。
至于建立新图,我是这样建立的(如图)
另外推荐一篇文章:Menci的博客
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = INT_MAX;
const int maxn = 305 * 4;
const int M = 10000;
struct edge1 {
int from;
int to;
int low;
int high;
int cost;
};
vector<edge1> es;
struct edge {
int from;
int to;
int cap;
int cost;
};
vector<edge> edges;
vector<int> G[maxn];
inline void add_edge(int from, int to, int cap, int cost) {
edges.push_back((edge){from, to, cap, cost});
edges.push_back((edge){to, from, 0, -cost});
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
inline int read() {
char c = getchar();
int f = 1, x = 0;
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = getchar();
}
while (isdigit(c))
x = x * 10 + c - '0', c = getchar();
return x * f;
}
int n, s, t, V;
void build_network() {
for (int i = 0; i < es.size(); i++) {
edge1 &e = es[i];
add_edge(e.from, e.to, e.high - e.low, e.cost);
add_edge(e.from, e.to, e.low, e.cost - M);
}
}
int dist[maxn], a[maxn], pree[maxn], inq[maxn];
bool spfa(int s, int t, int &cost) {
for (int i = 0; i < V; i++)
dist[i] = inf;
memset(pree, 0, sizeof(pree));
memset(inq, 0, sizeof(inq));
a[s] = inf;
dist[s] = 0;
queue<int> q;
q.push(s);
inq[s] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
inq[u] = 0;
for (int i = 0; i < G[u].size(); i++) {
edge &e = edges[G[u][i]];
if (e.cap > 0 && dist[e.to] > dist[u] + e.cost) {
pree[e.to] = G[u][i];
dist[e.to] = dist[u] + e.cost;
a[e.to] = min(e.cap, a[u]);
if (!inq[e.to]) {
q.push(e.to);
inq[e.to] = 1;
}
}
}
}
if (dist[t] >= inf)
return false;
cost += a[t] * dist[t];
int u = t;
while (u != s) {
edges[pree[u]].cap -= a[t];
edges[pree[u] ^ 1].cap += a[t];
u = edges[pree[u]].from;
}
return true;
}
int mcmf(int s, int t) {
int cost = 0;
while (spfa(s, t, cost))
;
// cout << "Hey:" << cost << endl;
return cost;
}
int main() {
// freopen("input", "r", stdin);
scanf("%d", &n);
s = 0, t = n + 1, V = t + 1;
int cnt = 0;
es.push_back((edge1){s, 1, 0, inf, 0});
for (int i = 1; i <= n; i++) {
int k;
k = read();
for (int j = 0; j < k; j++) {
int a, b;
a = read();
b = read();
es.push_back((edge1){i, a, 1, inf, b});
cnt += 1;
}
es.push_back((edge1){i, t, 0, inf, 0});
}
build_network();
int ans = mcmf(s, t);
printf("%d", ans + M * cnt);
}
[bzoj3876][AHOI2014]支线剧情——上下界费用流的更多相关文章
- BZOJ 3876: [Ahoi2014]支线剧情 [上下界费用流]
3876: [Ahoi2014]支线剧情 题意:每次只能从1开始,每条边至少经过一次,有边权,求最小花费 裸上下界费用流...每条边下界为1就行了 注意要加上下界*边权 #include <io ...
- [AHOI2014&&JSOI2014][bzoj3876] 支线剧情 [上下界费用流]
题面 传送门 思路 转化模型:给一张有向无环图,每次你可以选择一条路径走,花费的时间为路径上边权的总和,问要使所有边都被走至少一遍(可以重复),至少需要花费多久 走至少一遍,等价于覆盖这条边 也就是说 ...
- BZOJ 3876 支线剧情 | 有下界费用流
BZOJ 3876 支线剧情 | 有下界费用流 题意 这题题面搞得我看了半天没看懂--是这样的,原题中的"剧情"指的是边,"剧情点"指的才是点. 题面翻译过来大 ...
- 【BZOJ3876】[Ahoi2014]支线剧情 有上下界费用流
[BZOJ3876][Ahoi2014]支线剧情 Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩 ...
- 【有源汇上下界费用流】BZOJ 3876 [Ahoi2014]支线剧情
题目链接: http://www.lydsy.com:808/JudgeOnline/problem.php?id=3876 题目大意: 给定一张拓扑图(有向无环图),每条边有边权,每次只能从第一个点 ...
- BZOJ.1927.[SDOI2010]星际竞速(无源汇上下界费用流SPFA /最小路径覆盖)
题目链接 上下界费用流: /* 每个点i恰好(最少+最多)经过一次->拆点(最多)+限制流量下界(i,i',[1,1],0)(最少) 然后无源汇可行流 不需要源汇. 注: SS只会连i',求SS ...
- BZOJ2324 ZJOI2011营救皮卡丘(floyd+上下界费用流)
虽然不一定每次都是由编号小的点向编号大的走,但一个人摧毁的顺序一定是从编号小的到编号大的.那么在摧毁据点x的过程中,其只能经过编号小于x的点.并且这样一定合法,因为可以控制其他人先去摧毁所经过的点.那 ...
- 【BZOJ2055】80人环游世界 有上下界费用流
[BZOJ2055]80人环游世界 Description 想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么 一个 ...
- 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...
随机推荐
- fiddler显示出服务器IP方法
fiddler的配置中是看不到服务器的IP的 1.打开进入fiddler界面,按快捷键ctrl+r 或者按照图中点击,进入customrules.js文件里. 2.在customrules.js文件里 ...
- MySQL☞where与like
1.无条件查询语句(查询表中所有数据) select * from 表名 2.查询某些列的数据(指定列) select 列名1,列名2,列名3 from 表名 3.条件查询语句 select 列名1, ...
- 树莓派3_win10下使用"远程桌面连接"与树莓派通信(使用VNC实现连接后)
-----------------------------------------------------------学无止境------------------------------------- ...
- resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)
#!/usr/bin/python import os import json import subprocess from cloudinit.sources.DataSourceConfigDri ...
- 分布式文件系统---GlusterF
1.1 分布式文件系统 1.1.1 什么是分布式文件系统 相对于本机端的文件系统而言,分布式文件系统(英语:Distributed file system, DFS),或是网络文件系统(英语:Ne ...
- 对TPR(真正例率) 与 FPR(反正例率)的理解
将测试样本进行排序,“最可能”是正例的排在最前面,“最不可能”是正例的排在最后面. 分类过程就相当于在这个排序中以某个“截断点”(见图中阈值)将样本分为两部分,前一部分判作正例,后一部分判作反例. 我 ...
- oracle INSERT INTO多个值
稍微熟悉Oracle的都知道,如果我们想一条SQL语句向表中插入多个值的话,如果INSERT INTO 某表 VALUES(各个值),VALUES(各个值),.....;这样会报错的,因为oracle ...
- MapReduce 并行编程理论基础
对于mapreduce这一并行计算模型,一直以来都不是很清楚其具体的执行细节,今天看了学院一位老师的实验指导书,对这一过程有了一个初步的理解,特别是map阶段和reduce阶段,所以做了一份笔记,现在 ...
- vue里的this
vue中methods对象里的函数, this指向的都是当前实例或者组件.
- oracle 引用类型声明