洛谷P4043 支线剧情
题意:给定DAG,通过每条边需要时间。
从某号点回到1号点不需要时间。
从1号点出发,求最少要多久才能走完所有边。
解:
有源汇有上下界最小费用可行流。
直接连边,费用为时间,下界为1,无上界。
每个点都可能是终点,往t连边。
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring> const int N = , M = , INF = 0x3f3f3f3f; struct Edge {
int nex, v, c, len;
}edge[M << ]; int top = ; int e[N], d[N], vis[N], pre[N], flow[N], ot[N];
std::queue<int> Q; inline void add(int x, int y, int z, int w) {
top++;
edge[top].v = y;
edge[top].c = z;
edge[top].len = w;
edge[top].nex = e[x];
e[x] = top; top++;
edge[top].v = x;
edge[top].c = ;
edge[top].len = -w;
edge[top].nex = e[y];
e[y] = top;
return;
} inline bool SPFA(int s, int t) {
memset(d, 0x3f, sizeof(d));
d[s] = ;
flow[s] = INF;
vis[s] = ;
Q.push(s);
while(!Q.empty()) {
int x = Q.front();
Q.pop();
vis[x] = ;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(edge[i].c && d[y] > d[x] + edge[i].len) {
d[y] = d[x] + edge[i].len;
pre[y] = i;
flow[y] = std::min(flow[x], edge[i].c);
if(!vis[y]) {
vis[y] = ;
Q.push(y);
}
}
}
}
return d[t] < INF;
} inline void update(int s, int t) {
int temp = flow[t];
while(t != s) {
int i = pre[t];
edge[i].c -= temp;
edge[i ^ ].c += temp;
t = edge[i ^ ].v;
}
return;
} inline int solve(int s, int t, int &cost) {
int ans = ;
cost = ;
while(SPFA(s, t)) {
ans += flow[t];
cost += flow[t] * d[t];
update(s, t);
}
return ans;
} int main() {
int n, cost = ;
scanf("%d", &n);
int ss = n + , tt = n + , t = n + ;
for(int i = , x, y, z; i <= n; i++) {
scanf("%d", &x);
for(int j = ; j <= x; j++) {
scanf("%d%d", &y, &z);
// i -> y z
add(i, y, INF, z);
ot[i]++;
ot[y]--;
cost += z;
}
add(i, t, INF, );
}
for(int i = ; i <= n; i++) {
if(ot[i] > ) {
add(i, tt, ot[i], );
}
else {
add(ss, i, -ot[i], );
}
}
add(t, , INF, );
int ans;
solve(ss, tt, ans);
printf("%d", ans + cost);
return ;
}
AC代码
洛谷P4043 支线剧情的更多相关文章
- 洛谷P4135 Ynoi2016 掉进兔子洞 (带权bitset?/bitset优化莫队 模板) 题解
题面. 看到这道题,我第一反应就是莫队. 我甚至也猜出了把所有询问的三个区间压到一起处理然后分别计算对应询问答案. 但是,这么复杂的贡献用什么东西存?难道要开一个数组 query_appear_tim ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
- 洛谷P1371 NOI元丹
P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交 讨论 题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...
- 洛谷P1538迎春舞会之数字舞蹈
题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...
- 洛谷八月月赛Round1凄惨记
个人背景: 上午9:30放学,然后因为学校举办读书工程跟同学去书城选书,中午回来开始打比赛,下午又回老家,中间抽出一点时间调代码,回家已经8:50了 也许是7月月赛时“连蒙带骗”AK的太幸运然而因同学 ...
随机推荐
- Thrift_简介(基于C#)
//Server: TProtocolFactory ProtocolFactory = new TBinaryProtocol.Factory(true, true); TTransportFact ...
- [LeetCode] 307. Range Sum Query - Mutable 解题思路
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- Scrum Meeting NO.9
Scrum Meeting No.9 1.会议内容 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构:前端通讯模块改为HttpClient+Json √ 2 "我" ...
- Linux内核分析作业五
扒开系统调用的三层皮(下) 给MenuOS增加time和time-asm命令 步骤 rm menu -rf //强制删除 git clone http://github.com/menging/men ...
- JAVA常用工具类汇总
一.功能方法目录清单: 1.getString(String sSource)的功能是判断参数是否为空,为空返回"",否则返回其值: 2.getString(int iSource ...
- SpringBoot-简单实例
在进行实例之前,首先须确保电脑环境变量已经配置好,包括jdk.maven.此文章不做描述,不清楚自行百度. 第一步:来到springboot官网(https://start.spring.io/)下载 ...
- C#-ToString格式化
Int.ToString(format): 格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的有效位数或小数位数,具体如下: 格式说明符 说明 ...
- Difference between prop and attr in different version of jquery
jQuery <1.9$('#inputId').attr('readonly', true); jQuery 1.9+$('#inputId').prop('readonly', true); ...
- 表格属性和BFC(block framing content)
th和tr都是表示列但是 th有一个居中加粗的效果. 表单是由 : 1表单域:<form name=" " method="get/post" acti ...
- NF5280M4 安装 Win2016 的方法
1. 前提条件, 硬盘大于2T, 2. 必须使用最新版本的 Win2016 首先 win2016的可用序列号 • Windows Server 数据中心 CB7KF-BWN84-R7R2Y-793K2 ...