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 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.
 
题目大意:题目好长……大概是说,给你一棵树,每条边有一个容量。然后m个询问,每个询问是互相独立的,给你两个点S T,一个预算K,建一条容量为1的新边的费用A,将某一条现有的边容量扩大1的费用B。问从S到T在预算允许的情况下最大流是多少。
思路:首先,如果扩展边,肯定在S和T之间扩展最好,若A≤B,那么直接在S和T之间建$\lfloor K / A \rfloor$条边即可,再求从A到B的最大流(怎么求等下讲)。若A>B,有两种方案。方案一,建一条S到T的边,然后扩展$\lfloor K / B \rfloor$次,求A到B最大流。方案2,在S到T的路径中扩展容量。取两个方案的最大值。
因为从S到T只有一条路,那么我们只需要知道从S到T的那条路径上,最小的边是多少,就可以求出最大流。这里可以用主席树,每个点使用父节点的历史版本(这里不介绍主席树,不懂可以去翻我以前的blog),建一棵新的线段树。然后S结点+T结点-2*LCA就可以得到S到T的线段树(LCA可以用RMQ来求,也可以像我这样用离线的tarjan),就是主席树求第1小而已。
最后的问题就是A>B的方案2,在得到S到T的线段树之后,我们最多扩展边$\lfloor K / B \rfloor$次。那么,像二分一样,在线段树上走,若可以满足小于mid的边都扩充到mid,那么往右儿子走,若不能满足,则往左儿子走,走到叶子即可。
PS:我代码中的链表是有两种用法的,一种是树上的边,另一种是tarjan用的链表。
PS:我的线段树好像有的left初始化为1有的初始化为0,但是没有WA,大概是这种题目很难搞出这么精细的WA吧……懒得改了……
 
代码(640MS):
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 using namespace std;
  6 typedef long long LL;
  7 
  8 const int MAXC = 10000;
  9 const int MAXN = 100010;
 10 const int MAXV = 2 * 100010;
 11 const int MAXE = 2 * 200010;
 12 
 13 int head[MAXV], fa[MAXV];
 14 bool vis[MAXV];
 15 int to[MAXE], next[MAXE], weight[MAXE];
 16 int T, n, m, ecnt;
 17 
 18 void init() {
 19     memset(head, 0, sizeof(head));
 20     memset(vis, 0, sizeof(vis));
 21     for(int i = 1; i <= n; ++i) fa[i] = i;
 22     ecnt = 2;
 23 }
 24 
 25 void add_edge(int u, int v, int c) {
 26     to[ecnt] = v; weight[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
 27     to[ecnt] = u; weight[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
 28 }
 29 
 30 struct QUERY {
 31     int s, t, k, a, b, lca;
 32     void read(int i) {
 33         scanf("%d%d%d%d%d", &s, &t, &k, &a, &b);
 34         add_edge(s + n, t + n, i);
 35     }
 36 } Query[MAXV];
 37 
 38 int get_set(int x) {
 39     return fa[x] == x ? x : fa[x] = get_set(fa[x]);
 40 }
 41 
 42 void dfs_lca(int u, int f) {
 43     for(int p = head[u]; p; p = next[p]) {
 44         int &v = to[p];
 45         if(v == f) continue;
 46         dfs_lca(v, u);
 47         fa[v] = u;
 48     }
 49     vis[u] = true;
 50     for(int p = head[u + n]; p; p = next[p]) {
 51         int v = to[p] - n;
 52         if(vis[v]) Query[weight[p]].lca = get_set(v);
 53     }
 54 }
 55 
 56 struct Node {
 57     int L, R, sum, cnt;
 58 } tree[20 * MAXN];
 59 int root[MAXV], Tcnt;
 60 
 61 void init_Tree() {
 62     root[1] = 0;
 63     Tcnt = 1;
 64 }
 65 
 66 void insert(int &x, int left, int right, int &val) {
 67     tree[Tcnt] = tree[x]; x = Tcnt++;
 68     ++tree[x].cnt; tree[x].sum += val;
 69     if(left == right) return ;
 70     int mid = (left + right) >> 1;
 71     if(val <= mid) insert(tree[x].L, left, mid, val);
 72     else insert(tree[x].R, mid + 1, right, val);
 73 }
 74 
 75 void dfs_build_tree(int u, int f, int w) {
 76     root[u] = root[f];
 77     if(f) insert(root[u], 0, MAXC, w);
 78     for(int p = head[u]; p; p = next[p]) {
 79         int &v = to[p];
 80         if(v == f) continue;
 81         dfs_build_tree(v, u, weight[p]);
 82     }
 83 }
 84 
 85 int query(int x, int y, int lca, int left, int right, int k) {
 86     if(left == right) return left;
 87     int t = tree[tree[x].L].cnt + tree[tree[y].L].cnt - 2 * tree[tree[lca].L].cnt;
 88     int mid = (left + right) >> 1;
 89     if(k <= t) return query(tree[x].L, tree[y].L, tree[lca].L, left, mid, k);
 90     else return query(tree[x].R, tree[y].R, tree[lca].R, mid + 1, right, k - t);
 91 }
 92 
 93 int query(int x, int y, int lca, int cap) {
 94     int l = 1, r = MAXC, cnt = 0, sum = 0;
 95     while(l < r) {
 96         int mid = (l + r) >> 1;
 97         int tmp_cnt = tree[tree[x].L].cnt + tree[tree[y].L].cnt - 2 * tree[tree[lca].L].cnt;
 98         int tmp_sum = tree[tree[x].L].sum + tree[tree[y].L].sum - 2 * tree[tree[lca].L].sum;
 99         if((cnt + tmp_cnt) * mid - (sum + tmp_sum) > cap)
100             x = tree[x].L, y = tree[y].L, lca = tree[lca].L, r = mid;
101         else x = tree[x].R, y = tree[y].R, lca = tree[lca].R, l = mid + 1, cnt += tmp_cnt, sum += tmp_sum;
102     }
103     return l - 1;
104 }
105 
106 int main() {
107     scanf("%d", &T);
108     for(int t = 1; t <= T; ++t) {
109         scanf("%d%d", &n, &m);
110         init();
111         for(int i = 1; i < n; ++i) {
112             int u, v, c;
113             scanf("%d%d%d", &u, &v, &c);
114             add_edge(u, v, c);
115         }
116         for(int i = 1; i <= m; ++i) Query[i].read(i);
117         printf("Case #%d:\n", t);
118         dfs_lca(1, 0);
119         //for(int i = 1; i <= m; ++i) printf("%d\n", Query[i].lca);
120         init_Tree();
121         dfs_build_tree(1, 0, 0);
122         //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));
123         for(int i = 1; i <= m; ++i) {
124             int ans = query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], 0, MAXC, 1);
125             if(Query[i].a < Query[i].b) ans += Query[i].k / Query[i].a;
126             else {
127                 if(Query[i].k >= Query[i].a) ans += (Query[i].k - Query[i].a) / Query[i].b + 1;
128                 ans = max(ans, query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], Query[i].k / Query[i].b));
129             }
130             printf("%d\n", ans);
131         }
132     }
133 }
View Code

 

posted @ 2013-09-23 18:34  Oyking  阅读(405)  评论(0编辑  收藏  举报