模拟(堆):USACO Jan11 瓶颈
题目描述
Farmer John is gathering the cows. His farm contains a network of N (1 <= N <= 100,000) fields conveniently numbered 1..N and connected by N-1 unidirectional paths that eventually lead to field 1. The fields and paths form a tree.
Each field i > 1 has a single one-way, exiting path to field P_i, and currently contains C_i cows (1 <= C_i <= 1,000,000,000). In each time unit, no more than M_i (0 <= M_i <= 1,000,000,000) cows can travel from field i to field P_i (1 <= P_i <= N) (i.e., only M_i cows can traverse the path).
Farmer John wants all the cows to congregate in field 1 (which has no limit on the number of cows it may have). Rules are as follows:
Time is considered in discrete units.
Any given cow might traverse multiple paths in the same time unit. However, no more than M_i total cows can leave field i (i.e., traverse its exit path) in the same time unit.
- Cows never move *away* from field #1.
In other words, every time step, each cow has the choice either to
a) stay in its current field
b) move through one or more fields toward field #1, as long as the bottleneck constraints for each path are not violated
Farmer John wants to know how many cows can arrive in field 1 by certain times. In particular, he has a list of K (1 <= K <= 10,000) times T_i (1 <= T_i <= 1,000,000,000), and he wants to know, for each T_i in the list, the maximum number of cows that can arrive at field 1 by T_i if scheduled to optimize this quantity.
Consider an example where the tree is a straight line, and the T_i list contains only T_1=5, and cows are distibuted as shown:
Locn: 1---2---3---4 <-- Pasture ID numbers
C_i: 0 1 12 12 <-- Current number of cows
M_i: 5 8 3 <-- Limits on path traversal; field 1 has no limit since it has no exit
The solution is as follows; the goal is to move cows to field 1:
Tree: 1---2---3---4
t=0 0 1 12 12 <-- Initial state
t=1 5 4 7 9 <-- field 1 has cows from field 2 and 3 t=2 10 7 2 6
t=3 15 7 0 3
t=4 20 5 0 0
t=5 25 0 0 0
Thus, the answer is 25: all 25 cows can arrive at field 1 by time t=5.
Farmer John有一张N个农场构成的网络(1 <= N <= 100,000) ,我们将农场标记为1N。 农场被N-1条单向道路连接,保证从任何一个农场可以到达1号农场。FJ想让奶牛到1号农场集中 (P.S. 至于要做什么我也不知道)。 对于每个农场i > 1都有一条单独的单向道路通往P_i,并且这个农场里有C_i只奶牛 (1 <= C_i <= 1,000,000,000)。
在每个单位时间里,这条道路允许不超过M_i (0 <= M_i <= 1,000,000,000)只奶牛从农场i走到农场P_i (1 <= P_i <= N)。 Farmer John 想让所有的奶牛都集中在1号农场(农场容纳奶牛的数量是没有限制的)。 下面是奶牛集中到1号农场过程的规则:
我们认为时间是离散的 * 任何奶牛都可以在一个单位时间里走过任意多条道路。但是,必须满足每条道路的上限M_i。
- 奶牛从来不会离开1号农场。 换句话说,每一个单位时间,每只奶牛可以选择下面行动之一:
a) 留在当前的农场
b) 经过一条或者多条道路,向1号农场移动。
同样,需要满足每条道路的上限M_i。
FJ想知道有多少奶牛可以在某个特定的时刻到达1号农场。
特别的,他有一张列着K (1 <= K <= 10,000) 个时间T_i (1 <= T_i <= 1,000,000,000)的单子,他想知道对于每个T_i,如果采用最优 的策略在这个时刻结束时最多能有多少奶牛到达1号农场。
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and K
- Lines 2..N: Line i (not i+1) describes field i with three
space-separated integers: P_i, C_i, and M_i
- Lines N+1..N+K: Line N+i contains a single integer: T_i
输出格式:
- Lines 1..K: Line i contains a single integer that is the maximum number of cows that can arrive at field 1 by time T_i.
输入输出样例
4 1
1 1 5
2 12 7
3 12 3
5
25
1 #include <algorithm>
2 #include <iostream>
3 #include <cstring>
4 #include <cstdio>
5 #include <queue>
6 typedef long long LL;
7 using namespace std;
8 const int N=100010;
9 struct Query{LL t,res;int id;}ask[N];
10 bool cmp1(Query x,Query y){return x.t<y.t;}
11 bool cmp2(Query x,Query y){return x.id<y.id;}
12 struct Node{
13 LL t;int x;Node(LL t_=0,int x_=0){t=t_;x=x_;}
14 friend bool operator<(Node a,Node b){return a.t>b.t;}
15 };
16 priority_queue<Node>q;
17 int fa[N],anc[N],lim[N];
18 LL cow[N],pass[N];
19 int Find(int x){
20 if(anc[x]==x)return x;
21 return anc[x]=Find(anc[x]);
22 }
23 int n,Q;
24 int main(){
25 freopen("bottleneck.in","r",stdin);
26 freopen("bottleneck.out","w",stdout);
27 scanf("%d%d",&n,&Q);
28 for(int i=1;i<=n;i++)
29 anc[i]=i;
30 for(int i=2;i<=n;i++){
31 scanf("%d",&fa[i]);
32 scanf("%lld",&cow[i]);
33 scanf("%lld",&lim[i]);
34 pass[fa[i]]-=lim[i];
35 pass[i]+=lim[i];
36 }
37 for(int i=1;i<=Q;i++){
38 scanf("%lld",&ask[i].t);
39 ask[i].id=i;
40 }sort(ask+1,ask+Q+1,cmp1);
41
42 for(int i=2;i<=n;i++)
43 if(pass[i]>0){
44 LL t=cow[i]/pass[i];
45 q.push(Node(t,i));
46 }
47
48 int p=1,x,tp;
49 while(!q.empty()&&p<=Q){
50 while(p<=Q&&ask[p].t<=q.top().t)
51 ask[p].res=cow[1]-pass[1]*ask[p].t,p++;
52 if(anc[q.top().x]!=q.top().x){q.pop();continue;}
53 x=q.top().x;tp=Find(fa[x]);cow[tp]+=cow[x];
54 pass[tp]+=pass[x];anc[x]=tp;
55 if(pass[tp]>0){
56 LL t=cow[tp]/pass[tp];
57 q.push(Node(t,tp));
58 }
59 q.pop();
60 }sort(ask+1,ask+Q+1,cmp2);
61 for(int i=1;i<=Q;i++)
62 printf("%lld\n",ask[i].res);
63 return 0;
64 }
模拟(堆):USACO Jan11 瓶颈的更多相关文章
- Java堆内存溢出模拟
先了解一下Java堆: 关于Java内存区域的分配,可以查看Java运行时数据区域一篇文章. Java堆是虚拟机内存管理中最大的一块区域,该区域是线程共享的,某Java进程中所有的线程都可以访问该区域 ...
- Eclipse Memory Analysis进行堆转储文件分析
生成堆转储文件 新建项目,设置Eclispe Java堆的大小: (1)限制Java堆大小:将最小值 -Xms参数与最大值-Xmx参数设置一样可避免堆的扩展 -Xmx20m -Xms2 ...
- USACO Section 1.1 Your Ride Is Here 解题报告
题目 问题描述 将字符串转变为数字,字母A对应的值为1,依次对应,字母Z对应的值为26.现在有一个字符串,将其中的每个字符转变为数字之后进行累乘,最终的结果对47求余数. 题目给你两个字符串,其中的字 ...
- 基本数据结构——堆(Heap)的基本概念及其操作
基本数据结构――堆的基本概念及其操作 小广告:福建安溪一中在线评测系统 Online Judge 在我刚听到堆这个名词的时候,我认为它是一堆东西的集合... 但其实吧它是利用完全二叉树的结构来维护一组 ...
- Java 伙伴系统(模拟)
参考:https://labrick.cc/2015/10/12/buddy-system-algorithm/ 代码过烂 不宜参考. output: [operating.entity.Heap@4 ...
- 堆排序(大顶堆、小顶堆)----C语言
堆排序 之前的随笔写了栈(顺序栈.链式栈).队列(循环队列.链式队列).链表.二叉树,这次随笔来写堆 1.什么是堆? 堆是一种非线性结构,(本篇随笔主要分析堆的数组实现)可以把堆看作一个数组,也可以被 ...
- Java伙伴系统(模拟)
参考:https://labrick.cc/2015/10/12/buddy-system-algorithm/ 代码过烂 不宜参考. output: [operating.entity.Heap@4 ...
- hihocoder 1109 堆优化的Prim算法
题目链接:http://hihocoder.com/problemset/problem/1109 , 最小生成树 + 堆优化(优先队列). 可以用优先队列,也可以自己手动模拟堆,为了练手,我两种都试 ...
- 数据结构中的堆(Heap)
堆排序总结 这是排序,不是查找!!!查找去找二叉排序树等. 满二叉树一定是完全二叉树,但完全二叉树不一定是满二叉树. 构建顶堆: a.构造初始堆 b.从最后一层非叶节点开始调整,一直到根节点 c.如果 ...
随机推荐
- 我眼中真正优秀的CTO
该文转自“肉饼铺子”.作者robbin是前JavaEye网站的创始人,TOPITCLUB互联网俱乐部发起人. 原文链接 现在进入正题,最近几个月,不断有人找我推荐CTO人选,这两年互联网创业和创投实 ...
- java web-----DAO设计模式(数据库访问)
一,DAO设计模式用于 j2ee 的数据层访问,包括五部分, 数据库连接类(包含数据库的连接与关闭操作的一个类), VO类(私有变量与数据库表格对应,接收数据库中表格各字段内容), DAO接口类(包含 ...
- 04_过滤器Filter_01_入门简述
[简述] Filter也称之为过滤器.通过Filter技术,对web服务器管理的所有资源(如:Jsp.Servlet.静态图片文件.静态HTML文件等)进行拦截,从而实现一些特殊的功能.例如实现URL ...
- 九度OJ 1078 二叉树遍历
题目地址:http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历 ...
- 设计模式之开篇(C#语法)
很长时间没有写文章了,前段时间写的C#语法糖分享得到有很多朋友支持,这个也使得我有信心继续分享下去,在这里非常感谢大家!这次开始给大家分享一下设计模式,我个人觉得设计模式也对于我们提高个人技术水平来说 ...
- c语言实现交换两个数的值
C语言中要实现交换两个数的值,可以有很多种方法,具体如下所述. 不使用中间变量: // 异或, a^=b^=a^=b; a ^= b; b ^= a; a ^= b; // 加减 a = a + b; ...
- [python] virtualenv下解决matplotlib中文乱码
1. 安装中文字体 一般系统自带wqy-microhei,其ttc文件位于/usr/share/fonts/truetype/wqy/wqy-microhei.ttc 2. 将ttc文件复制到pyth ...
- 玩转HTML5移动页面(动效篇)
原文:http://www.grycheng.com/?p=458 作为一名前端,在拿到设计稿时你有两种选择: 1.快速输出静态页面 2.加上高级大气上档次狂拽炫酷屌炸天的动画让页面动起来 作为一个有 ...
- iOS -- warnings
Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte t ...
- HTML5+CSS3鼠标悬停图片特效
点击预览效果 下载该特效: HTML5+CSS3鼠标悬停图片特效.zip 特效说明: 一款HTML5+CSS3鼠标悬停图片事件网页特效,集合了最流行鼠标悬停图片文字.图片动画移动 ...