HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)
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.
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.
Then for each day, output the maximum water Pfctgeorge can transfer from S and T with a budget of K.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL; const int MAXC = ;
const int MAXN = ;
const int MAXV = * ;
const int MAXE = * ; int head[MAXV], fa[MAXV];
bool vis[MAXV];
int to[MAXE], next[MAXE], weight[MAXE];
int T, n, m, ecnt; void init() {
memset(head, , sizeof(head));
memset(vis, , sizeof(vis));
for(int i = ; i <= n; ++i) fa[i] = i;
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; weight[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; weight[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} struct QUERY {
int s, t, k, a, b, lca;
void read(int i) {
scanf("%d%d%d%d%d", &s, &t, &k, &a, &b);
add_edge(s + n, t + n, i);
}
} Query[MAXV]; int get_set(int x) {
return fa[x] == x ? x : fa[x] = get_set(fa[x]);
} void dfs_lca(int u, int f) {
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
dfs_lca(v, u);
fa[v] = u;
}
vis[u] = true;
for(int p = head[u + n]; p; p = next[p]) {
int v = to[p] - n;
if(vis[v]) Query[weight[p]].lca = get_set(v);
}
} struct Node {
int L, R, sum, cnt;
} tree[ * MAXN];
int root[MAXV], Tcnt; void init_Tree() {
root[] = ;
Tcnt = ;
} void insert(int &x, int left, int right, int &val) {
tree[Tcnt] = tree[x]; x = Tcnt++;
++tree[x].cnt; tree[x].sum += val;
if(left == right) return ;
int mid = (left + right) >> ;
if(val <= mid) insert(tree[x].L, left, mid, val);
else insert(tree[x].R, mid + , right, val);
} void dfs_build_tree(int u, int f, int w) {
root[u] = root[f];
if(f) insert(root[u], , MAXC, w);
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
dfs_build_tree(v, u, weight[p]);
}
} int query(int x, int y, int lca, int left, int right, int k) {
if(left == right) return left;
int t = tree[tree[x].L].cnt + tree[tree[y].L].cnt - * tree[tree[lca].L].cnt;
int mid = (left + right) >> ;
if(k <= t) return query(tree[x].L, tree[y].L, tree[lca].L, left, mid, k);
else return query(tree[x].R, tree[y].R, tree[lca].R, mid + , right, k - t);
} int query(int x, int y, int lca, int cap) {
int l = , r = MAXC, cnt = , sum = ;
while(l < r) {
int mid = (l + r) >> ;
int tmp_cnt = tree[tree[x].L].cnt + tree[tree[y].L].cnt - * tree[tree[lca].L].cnt;
int tmp_sum = tree[tree[x].L].sum + tree[tree[y].L].sum - * tree[tree[lca].L].sum;
if((cnt + tmp_cnt) * mid - (sum + tmp_sum) > cap)
x = tree[x].L, y = tree[y].L, lca = tree[lca].L, r = mid;
else x = tree[x].R, y = tree[y].R, lca = tree[lca].R, l = mid + , cnt += tmp_cnt, sum += tmp_sum;
}
return l - ;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
init();
for(int i = ; i < n; ++i) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
for(int i = ; i <= m; ++i) Query[i].read(i);
printf("Case #%d:\n", t);
dfs_lca(, );
//for(int i = 1; i <= m; ++i) printf("%d\n", Query[i].lca);
init_Tree();
dfs_build_tree(, , );
//for(int i = 1; i <= m; ++i) printf("%d\n", query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], 0, MAXC, 1));
for(int i = ; i <= m; ++i) {
int ans = query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], , MAXC, );
if(Query[i].a < Query[i].b) ans += Query[i].k / Query[i].a;
else {
if(Query[i].k >= Query[i].a) ans += (Query[i].k - Query[i].a) / Query[i].b + ;
ans = max(ans, query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], Query[i].k / Query[i].b));
}
printf("%d\n", ans);
}
}
}
HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)的更多相关文章
- HDU 4729 An Easy Problem for Elfness 主席树
题意: 给出一棵树,每条边有一个容量. 有若干次询问:\(S \, T \, K \, A \, B\),求路径\(S \to T\)的最大流量. 有两种方法可以增大流量: 花费\(A\)可以新修一条 ...
- HDU 4729 An Easy Problem for Elfness(树链剖分边权+二分)
题意 链接:https://cn.vjudge.net/problem/HDU-4729 给你n个点,然你求两个点s和t之间的最大流.而且你有一定的钱k,可以进行两种操作 1.在任意连个点之间建立一个 ...
- HDU 4735 Little Wish~ lyrical step~(DLX搜索)(2013 ACM/ICPC Asia Regional Chengdu Online)
Description N children are living in a tree with exactly N nodes, on each node there lies either a b ...
- HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description There are N points in total. Every point moves in certain direction and certain speed. W ...
- HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...
- HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)
HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (J ...
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...
随机推荐
- 优雅的QSignleton (四) 通过属性器实现MonoSingleton
大家都出去过周六了,而我却在家写代码T.T... 接下来介绍通过属性器实现MonoSingleton. 代码如下: MonoSingletonProperty.cs namespace QFr ...
- Data Guard 知识 (来自网络)
更改DG工作模式前提参数得设定合理. Physical standby直接从主库接受archived log,然后直接做基于block的物理恢复(更新或调整变化的block),所以physical s ...
- JavaScript 基础(五) 函数 变量和作用域
函数定义和调用 定义函数,在JavaScript中,定义函数的方式如下: function abs(x){ if(x >=0){ return x; }else{ return -x; } } ...
- flex 遇到white-space:nowrap
背景,做一个前面图片宽度固定,后面宽度自适应,使用到了flex布局,但是想让后面div里文字不换行,超出以点点表示时,这时布局就乱了,查了下,原来flex布局与white-space:nowrap有影 ...
- cc++面试------17道经典面试题目分析
以下是C/C++面试题目,共计17个题目,其中涵盖了c的各种基础语法和算法, 以函数接口设计和算法设计为主.这17个题目在C/C++面试方面已经流行了多 年,大家需要抽时间掌握好,每一个题目后面附有参 ...
- c/c++面试总结---c语言基础算法总结2
c/c++面试总结---c语言基础算法总结2 算法是程序设计的灵魂,好的程序一定是根据合适的算法编程完成的.所有面试过程中重点在考察应聘者基础算法的掌握程度. 上一篇讲解了5中基础的算法,需要在面试之 ...
- 【mysql学习-1】
part-1: #use mysql;/*show tables;select * from user;use mysql;show databases;#create database db1; # ...
- MySQL快速生成连续整数
很多时候需要用到连续的id进行数据对比,如判断是否连续等问题.那么,生成连续整数的方式有多种,首先容易想到的是逐步循环,如果想生成1kw条记录,则需要循环1kw次进行插入,那么有没有其他方式呢,效率相 ...
- PHP入门笔记--基础语法二
一.函数 自定义函数 任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和类定义. <?php function foo() { function bar() { echo &qu ...
- JavaSE 第二次学习随笔(四)
---------------------------------------------------------------------------------------------------- ...