An Easy Problem for Elfness

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

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.

  感觉这道题就是一个笑话。

  本地对拍会爆,但HDU上AC了……

  考虑按深度建主席树,儿子承接父亲的信息,这样就可以在线段树上查询一条链的信息了。

  三种决策:不停地新加边;先加一条边,再不停扩容;不停扩容。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
#pragma comment(linker,"/STACK:1024000000,1024000000")
int cntE,fir[maxn],to[maxn*],nxt[maxn*],cap[maxn*];
int rt[maxn],ch[maxn*][],sum[maxn*],cal[maxn*],cnt;
int dep[maxn];
int max(int a,int b){return a>b?a:b;}
void addedge(int a,int b,int c){
nxt[++cntE]=fir[a];
fir[a]=cntE;
cap[cntE]=c;
to[cntE]=b;
} void Insert(int pre,int &rt,int l,int r,int g){
rt=++cnt;
ch[rt][]=ch[pre][];
ch[rt][]=ch[pre][];
sum[rt]=sum[pre]+;
cal[rt]=cal[pre]+r-g+;
if(l==r)return;
int mid=(l+r)>>;
if(mid>=g)Insert(ch[pre][],ch[rt][],l,mid,g);
else Insert(ch[pre][],ch[rt][],mid+,r,g);
} int ID[maxn],tot;
int mm[maxn*],Min[maxn*][]; void DFS(int x,int fa){
Min[ID[x]=++tot][]=x;
for(int i=fir[x];i;i=nxt[i])
if(to[i]!=fa){
dep[to[i]]=dep[x]+;
Insert(rt[x],rt[to[i]],,,cap[i]);
DFS(to[i],x);Min[++tot][]=x;
}
} void Prepare(){
for(int i=;i<=tot;i++){
if((i&(i-))==)mm[i]=mm[i-]+;
else mm[i]=mm[i-];
}
for(int k=;k<=mm[tot];k++)
for(int i=;i+(<<k)-<=tot;i++){
if(dep[Min[i][k-]]<dep[Min[i+(<<k-)][k-]])
Min[i][k]=Min[i][k-];
else
Min[i][k]=Min[i+(<<k-)][k-];
}
} int Lca(int x,int y){
if(ID[x]>ID[y])swap(x,y);
int k=mm[ID[y]-ID[x]+];
if(dep[Min[ID[x]][k]]<dep[Min[ID[y]-(<<k)+][k]])
return Min[ID[x]][k];
return Min[ID[y]-(<<k)+][k];
} #define num() sum[ch[r1][0]]+sum[ch[r2][0]]-2*sum[ch[lca][0]]
#define key() cal[ch[r1][0]]+cal[ch[r2][0]]-2*cal[ch[lca][0]] int Query1(int lca,int r1,int r2,int l,int r){
if(l==r)return l;
int mid=(l+r)>>;
if(num()>)
return Query1(ch[lca][],ch[r1][],ch[r2][],l,mid);
return Query1(ch[lca][],ch[r1][],ch[r2][],mid+,r);
} int Query2(int lca,int r1,int r2,int l,int r,int k,int t){
if(l==r)return l;
int mid=(l+r)>>,tmp;
tmp=key()+t*(mid-l+);
if(tmp>k)return Query2(ch[lca][],ch[r1][],ch[r2][],l,mid,k,t);
return Query2(ch[lca][],ch[r1][],ch[r2][],mid+,r,k-tmp,t+num());
} int T,n,Q,cas;
int s,t,k,a,b,ans;
void Init(){
printf("Case #%d:\n",++cas);
memset(sum,,sizeof(sum));
memset(cal,,sizeof(cal));
memset(fir,,sizeof(fir));
memset(Min,,sizeof(Min));
mm[]=-;cntE=cnt=tot=;
} int main(){
scanf("%d",&T);
while(T--){
Init();
scanf("%d%d",&n,&Q);
for(int i=,c;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
} DFS(,);
Prepare(); while(Q--){
scanf("%d%d",&s,&t);
scanf("%d%d%d",&k,&a,&b);
int lca=Lca(s,t),tmp;
tmp=Query1(rt[lca],rt[s],rt[t],,);
if(a<=b)
ans=tmp+k/a;
else{
ans=tmp;
if(k>=a)ans+=+(k-a)/b;
ans=max(ans,Query2(rt[lca],rt[s],rt[t],,,k/b,));
}
printf("%d\n",ans);
}
}
return ;
}

数据结构(主席树):HDU 4729 An Easy Problem for Elfness的更多相关文章

  1. HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...

  2. HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)

    Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...

  3. HDU 4729 An Easy Problem for Elfness 主席树

    题意: 给出一棵树,每条边有一个容量. 有若干次询问:\(S \, T \, K \, A \, B\),求路径\(S \to T\)的最大流量. 有两种方法可以增大流量: 花费\(A\)可以新修一条 ...

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

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

  5. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  6. [HDU4729]An Easy Problem for Elfness

    [HDU4729]An Easy Problem for Elfness 题目大意: 给你一棵\(n(n\le10^5)\)个点的树,树上每条边都有容量. \(m(m\le10^5)\)次询问,每次询 ...

  7. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  8. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. 数据结构(主席树):COGS 2211. 谈笑风生

    2211. 谈笑风生 ★★★★   输入文件:laugh.in   输出文件:laugh.out   简单对比时间限制:3 s   内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下 ...

随机推荐

  1. 11.2 afternoon

    #include<iostream> #include<cstdio> #define ll long long using namespace std; ll n,ans; ...

  2. Log4j(1.2.17) - hello world

    1. Maven 依赖 <dependencies> <dependency> <groupId>log4j</groupId> <artifac ...

  3. 使用JavaScriptSerializer序列化集合、字典、数组、DataTable为JSON字符串 分类: 前端 数据格式 JSON 2014-10-30 14:08 169人阅读 评论(0) 收藏

    一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...

  4. Objective-C中的分类与协议

    分类 在谈分类之前,我们可以先探究下,OC中为什么出现分类这种机制,有什么好处? 假设你接到一个大项目:计算两个整数的和,差.接到任务的你马上动手.编写代码如下: #import <Founda ...

  5. php中的JSON中文处理

    最近在PHP中要输出JSON,上网查了一下,对中文支持不太好,要不就先转成utf-8的编码,再用json_encode生成,客户端还要再utf-8转中文.对于网页已经用GB2312的服务器,不想这样折 ...

  6. SGU 226.Colored graph(最短路)

    时间限制:0.25s 空间限制:4M 题意: 给出一个n个节点,m条边的图,每条边都有标记了编号为1,2,3三种颜色之一,现在求从1号节点到n号节点的一条最短路径的长度,要求该路径中相邻的边没有相同的 ...

  7. 《find技巧》-“linux命令五分系列”之一

    一天一个命令,做个记录, 我要成大神,哈哈哈 本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linu ...

  8. magento站点还原到本地

    问题描述 首先将网站文件夹解压到xampp/htdocs/wenjianjia目录下,然后替换sql文件里的域名为localhost/wenjianjia.然后访问前台,正常.访问后台,出问题了 Ma ...

  9. 原生Js获取某个节点后面的第一个标签

    nextSlbling属性 获取某个节点后面的第一个节点(可能是标签 文本) 判断获取的节点是否为标签节点还是文本节点 window.onload=function(){ var pagecount= ...

  10. Swift-MJ

    1.声明变量 常量 声明变量:var age = 10 声明常量:let age = 10 (不加分号,除非多条语句写在同一行) 二进制前缀:0b(必须小写) 八进制前缀:0o 十六进制前缀:0x 2 ...