An Easy Problem for Elfness

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1235    Accepted Submission(s): 257

Problem Description
Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge water transmission network that covers the whole southwest China. To save the fund, there will be exactly one path between two cities.

Since the water every city provides and costs every day is different, he needs to transfer water from one particular city to another as much as possible in the next few days. However the pipes which connect the cities have a limited capacity for transmission. (Which means the water that transfer though the pipe should not exceed a particular amount) So he has to know the maximum water that the network can transfer in the next few days.

He thought it's a maximum flow problem, so he invites an expert in this field, Elfness (Also known as Xinhang senior sister) to help him figure it out.

Unlike Pfctgeorge, Elfness quickly finds that this problem is much easier than a normal maximum flow problem, and is willing to help Pfctgeorge.

"Oh well, this problem is not a tough one. We can ..."

Abruptly, Pfctgeorge's iPhone rings, and ... the ringtone is Mo Di Da Biao Ke.

"You can make that? Excellent! "Pfctgeorge hangs up his iPhone, and turns to Elfness.

"Here's good news for you. A construction team told me that every pipe's capacity can be extended for one day. And the price for extending one unit capacity varies from day to day. "

"Eh well, that's a good news for you, not me. Now it's rather like a minimum cost ow problem, right? But it's still not a tough one, let me have a think. "

After a few seconds' thought, Elfness comes up with a simple solution.

"Ok, we can solve it like... "

Abruptly, here comes Mo Di Da Biao Ke again.

"Seriously? You can build new pipes? Thank you very much. "

"OK, my dear Elfness, we got more good news. Another construction team said they can build one or more pipes between any two cities and their pipes are exactly like the original ones except that they only work for one day. And the capacity of the new pipes is only one, but they can be extended, too. Of course, their price to build a single pipe also varies in days. "

"You mean the new pipes can be extended too? Wow, things are getting more interesting. Give me a few minutes. "

Elfness takes out his new ultrabook which is awarded in VK cup and does some basic calculation.

"I get it. The problem can be solved ..."

Mo Di Da Biao Ke again, but this time it's from Elfness's phone.

"As you see, I have to go out. But I know someone else who can also solve this; I'll recommend this guy for you. "

And of course, that poor guy is YOU. Help Pfctgeorge solve his problem, and then the favorability about you from Elfness will raise a lot.

 
Input
The first line has a number T (T <= 10) , indicating the number of test cases.

The first line of each test case is two integers N (1 <= N <= 100000) and M (1 <= M <= 100000), indicating the number of the city that the original network connects and the number of days when Pfctgeorge needs to know about the maximum water transmissions. Then next N - 1 lines each describe a pipe that connects two cities. The format will be like U, V , cap (1 <= U, V <= N and 0 <= cap < 10000), which means the ids of the two cities the pipe connects and the transmission limit of the pipe. As is said in description, the network that the cities and pipes form is a tree (an undirected acyclic graph).

Then next M lines of the test case describe the information about the next few days. The format is like S, T, K, A, B(0 <= K <= 2^31 - 1, 1 <= A, B <= 2^31 - 1). S means the source of the water while T means the sink. K means the total budget in the day. A means the cost for a construction team to build a new pipe and B means the cost for a construction team to extend the capacity of a pipe.

I am glad to list the information of building a new pipe and extending the capacity.

1. Pfctgeorge can build a new pipe between any two cities, no matter they have been directly connected or not. Pfctgeorge can build more than one new pipe between any two cities.
2. The capacity of the pipe that was newly built is one.
3. Pfctgeorge can extend the capacity of any existed pipe including the newly built one and the original one.
4. Each time you extend the capacity of one pipe, the capacity of that pipe increases one.
5. The cost of building a new pipe is A and the cost of extending a pipe is B.
6. You can take any constructions in any times and the only limit is to make sure the total costs not exceed the budget.
7. All the work that construction team does only lasts one single day.

 
Output
For every case, you should output "Case #t:" at first, without quotes. The t is the case number starting from 1.
Then for each day, output the maximum water Pfctgeorge can transfer from S and T with a budget of K.
 
Sample Input
2
5 1
1 2 2
1 3 5
2 4 1
4 5 2
1 5 3 3 2
5 5
1 2 10
2 3 2
3 4 7
2 5 7
1 5 0 1 3
1 3 0 2 3
1 5 3 2 3
1 2 7 3 1
1 3 2 3 1
 
Sample Output
Case #1:
2
Case #2:
7
2
8
17
4

Hint

In the first sample case, you can extend the capacity of the pipe which connects city2 and city4 by one, or just build a new pipe between city2 and city4.

/*
hdu4729 树链剖分+二分 problem:
给你n个点,然你求两个点之间的最大流.而且你有一定的钱,可以进行两种操作
1.在任意连个点之间建立一个单位1的流,费用A
2.将原先的流扩大1个单位,费用B solve:
在最初始的图上面u,v之间的流flow即u->v边上的最小值
①如果A<=B,那么我们可以直接在两个目标点u,v之间建边.所以答案为 flow+k/A
②如果A>B
可以先建一条边然后不停地扩展 (flow-A)/B+1
or不停地给最小的边扩展,使u->v的最小值变大. 可以二分找到这个值
最开始是二分(1,10000)结果超时QAQ. 后来发现可以先求出(flow-A)/B+1,那么二分的时候就是((flow-A)/B+1,10000) hhh-2016-08-18 11:39:23
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 200100;
const int inf = 0x3f3f3f3f;
int head[maxn],tot,pos,son[maxn];
int top[maxn],fp[maxn],fa[maxn],dep[maxn],num[maxn],p[maxn];
int val[maxn];
int n;
struct Edge
{
int to,next,w;
} edge[maxn<<1]; void ini()
{
tot = 0,pos = 1;
clr(head,-1),clr(son,-1);
// clr(val,0);
} void add_edge(int u,int v,int w)
{
edge[tot].w = w,edge[tot].to = v,edge[tot].next = head[u],head[u] = tot++;
} void dfs1(int u,int pre,int d)
{
dep[u] = d;
fa[u] = pre,num[u] = 1;
// cout << "node:" << u<<endl;
for(int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if(v != pre)
{
val[v] = edge[i].w;
dfs1(v,u,d+1);
num[u] += num[v];
if(son[u] == -1 || num[v] > num[son[u]])
son[u] = v;
}
}
} void getpos(int u,int sp)
{
top[u] = sp;
p[u] = pos++;
fp[p[u]] = u;
if(son[u] == -1)return ;
getpos(son[u],sp);
for(int i = head[u]; ~i ; i = edge[i].next)
{
int v = edge[i].to;
if(v != son[u] && v != fa[u])
getpos(v,v);
}
} struct node
{
int l,r,mid;
ll Min;
} tree[maxn << 2];
void push_up(int i)
{
tree[i].Min = min(tree[lson].Min,tree[rson].Min);
}
void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].Min = inf;
tree[i].mid=(l+r) >>1;
if(l == r)
{
tree[i].Min = val[fp[l]];
// cout << fp[l] <<" " <<val[fp[l]]<<endl;
return;
}
build(lson,l,tree[i].mid);
build(rson,tree[i].mid+1,r);
push_up(i);
} void update(int i,int k,int val)
{
if(tree[i].l == k && tree[i].r == k)
{
tree[i].Min = val;
return;
}
int mid = tree[i].mid;
if(k <= mid) update(lson,k,val);
else update(rson,mid,val);
push_up(i);
}
ll query(int i,int l,int r)
{
// cout <<"l:"<< l <<" r:"<<r <<" min:"<< tree[i].Min<<endl;
if(tree[i].l >= l && tree[i].r <= r)
return tree[i].Min;
int mid = tree[i].mid;
if(r <= mid)
return query(lson,l,r);
else if(l > mid)
return query(rson,l,r);
else
{
return min(query(lson,l,mid),query(rson,mid+1,r));
}
}
ll find_flow(int u,int v)
{
int f1 = top[u],f2 = top[v];
ll tmp = inf;
// cout <<f1 <<" " <<f2 <<endl;
while(f1 != f2)
{
if(dep[f1] < dep[f2])
{
swap(f1,f2),swap(u,v);
}
tmp = min(tmp,query(1,p[f1],p[u]));
u = fa[f1],f1 = top[u];
}
if(u == v) return tmp;
if(dep[u] > dep[v]) swap(u,v);
// cout << son[u] << " " <<v <<endl;
return min(tmp,query(1,p[son[u]],p[v]));
}
int allnum = 0;
bool can_do(int i,int l,int r,int mid)
{
if(tree[i].l >= l && tree[i].r <= r && tree[i].Min >= mid)
{
return true;
}
if(tree[i].l == tree[i].r)
{
// cout << tree[i].Min <<" " <<mid<<endl;
if(tree[i].Min >= mid)
return true;
allnum -= (mid-tree[i].Min);
if(allnum>= 0) return true;
return false;
}
if(r <= tree[i].mid)
return can_do(lson,l,r,mid);
else if(l > tree[i].mid)
return can_do(rson,l,r,mid);
else
return can_do(lson,l,tree[i].mid,mid)&&can_do(rson,tree[i].mid+1,r,mid);
} bool find_flag(int u,int v,int mid)
{
int f1 = top[u],f2 = top[v];
while(f1 != f2)
{
if(dep[f1] < dep[f2])
{
swap(f1,f2),swap(u,v);
}
if(!can_do(1,p[f1],p[u],mid))
return false;
u = fa[f1],f1 = top[u];
}
if(u == v) return true;
if(dep[u] > dep[v]) swap(u,v);
return can_do(1,p[son[u]],p[v],mid);
} int a[maxn]; int main()
{
// freopen("in.txt","r",stdin);
int T,cas = 1;
int x,y,k,a,b;
int m,u,v,w;
scanf("%d",&T);
while(T--)
{
ini();
scanf("%d%d",&n,&m);
// cout << n <<" " <<m <<endl;
for(int i =1; i < n; i++)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
dfs1(1,0,0);
getpos(1,1);
build(1,0,pos-1);
// char op[10];
printf("Case #%d:\n",cas++);
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d%d%d",&x,&y,&k,&a,&b);
ll flow = find_flow(x,y);
// cout <<"flow:"<<flow <<endl;
if(k < min(a,b))
{
printf("%I64d\n",flow);
}
else if(a <= b)
{
printf("%I64d\n",flow+(ll)k/a);
}
else
{
ll ans = flow;
if(k > a)
ans += (k-a)/b+1;
int l = ans,r = 10000;
while(l <= r)
{
int mid = (l+r)>>1;
allnum = k/b;
if(find_flag(x,y,mid))
{
ans = mid,l = mid + 1;
}
else
r = mid - 1;
}
printf("%I64d\n",ans);
}
}
}
return 0;
}

  

hdu4729 树链剖分+二分的更多相关文章

  1. NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)

    Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...

  2. BZOJ 4551[Tjoi2016&Heoi2016]树(树链剖分+二分)

    Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记 ...

  3. BZOJ 4326 树链剖分+二分+差分+记忆化

    去年NOIP的时候我还不会树链剖分! 还是被UOJ 的数据卡了一组. 差分的思想还是很神啊! #include <iostream> #include <cstring> #i ...

  4. bzoj 4326: NOIP2015 运输计划【树链剖分+二分+树上差分】

    常数巨大,lg上开o2才能A 首先预处理出运输计划的长度len和lca,然后二分一个长度w,对于长度大于w的运输计划,在树上差分(d[u]+1,d[v]+1,d[lca]-2),然后dfs,找出所有覆 ...

  5. bzoj 4326: NOIP2015 运输计划(二分+树链剖分)

    传送门 题解: 树链剖分快速求解任意两点间的路径的权值和: 然后,二分答案: 此题的难点是如何快速求解重合路径? 差分数组可以否??? 在此之前先介绍一下相关变量: int fa[maxn]; int ...

  6. HDU 4729 An Easy Problem for Elfness(树链剖分边权+二分)

    题意 链接:https://cn.vjudge.net/problem/HDU-4729 给你n个点,然你求两个点s和t之间的最大流.而且你有一定的钱k,可以进行两种操作 1.在任意连个点之间建立一个 ...

  7. bzoj1146整体二分+树链剖分+树状数组

    其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...

  8. BZOJ_4326_[NOIP2015]_运输计划_(二分+LCA_树链剖分/Tarjan+差分)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=4326 给出一棵带有边权的树,以及一系列任务,任务是从树上的u点走到v点,代价为u到v路径上的权 ...

  9. bzoj4326 树链剖分 + 线段树 // 二分 lca + 树上差分

    https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题意:N个点的树上给M条树链,问去掉一条边的权值之后所有树链长度和的最大值最小是多少. 首先 ...

随机推荐

  1. 20162330 实验一 《Java开发环境的熟悉》 实验报告

    2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验一 <Java开发环境的熟悉> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1623 ...

  2. splinter web测试框架

    1.安装谷歌浏览器驱动(windows把驱动解压放在Python.exe同级目录即可) http://chromedriver.storage.googleapis.com/index.html 注意 ...

  3. JAVA反射机制基础概念

    反射机制:所谓的反射机制就是java语言在运行时拥有一项自观的能力.通过这种能力可以彻底的了解自身的情况为下一步的动作做准备.下面具体介绍一下java的反射机制.这里你将颠覆原来对java的理解. J ...

  4. Beta冲刺Day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  5. MySQL binlog 日志

    一:MySQL 日志的三种类型: statement.row.mix 格式.推荐使用row格式. 怎么设置自己的日志格式呢? 1. set globle binlog_format='MIXED' 2 ...

  6. redis入门(06)各种类型的操作命令

    Redis 字符串命令下表列出了常用的 redis 字符串命令:序号 命令及描述1 SET key value 设置指定 key 的值2 GET key 获取指定 key 的值.3 GETRANGE ...

  7. python入门(12)dict

    python入门(12)dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例 ...

  8. android- 远程调试

    最近由于要在另外一台android设备上调试代码,在本机PC上查看其log.两台机器离的比较远, 无法用usb直接连接,于是在网上找了很多资料,最找使用adb connect方法解决了该问题.解决过程 ...

  9. Python学习之参数

    参数 # coding=utf-8 # 函数的参数 def power(x): return x * x; print power(5) 修改后 def power_1(x,n=2): #默认参数可以 ...

  10. python flask框架 tempates 模版的使用

    在py文件同级下 建立templates文件夹,再文件夹中编写html文件 1 向模版中传递参数: ''' 1 向模板传送 参数 ''' @app.route('/') def index(): na ...