题目描述:

Another attempt of revising toll system is rejected by the people of Byteland. The Government is inbig trouble now. They are seeking help of Hashmat, the brave warrior and the great programmer ofByteland to solve the problem. Hashmat has come up with a new idea to fix this toll managementproblem.There are N cities and M bidirectional highways among these cities. The government proposedsome toll (may be different toll for different highways) for each of the highways. But people think theyare being over charged. Hashmat’s idea was to make both the people and the Government happy. Inhis plan he decided to keep the toll of the highways as it is in Government’s proposal but he wouldpublish a special set of N − 1 highways. There are two conditions for a set of N − 1 highways to bespecial. These N − 1 highways have to connect all the cities and a person will be able to go from acity to any other city spending minimum toll in any of these highways. Please note, using these specialhighways does not guarantee minimum sum of toll but it guarantees you minimum individual toll.People of Byteland are happy with the idea of special highway, but the Government is not happyas they want more toll from the highway sector. They called up a meeting and formed a committee tofind two values for all the highways. Let these values be Ai and Bi for the i-th highway and defined asfollows:1) Ai
The maximum amount of toll they can add to the i-th highway so that Hashmat’s set remains
special.
2) Bi
The maximum amount of toll they can decrease from the i-th highway so that Hashmat’s set
remains special.
In other words, if Ci
is the current toll of the i-th highway, then if the Government updates the toll
of the highway to Ci + Ai (or Ci − Bi), Hashmat’s set remains special. Please note, while finding out
Ai and Bi other tolls remain unchanged.
This time Hashmat does not want to help the Government. He thinks this is a conspiracy against
the people of Byteland. So they came to you. Will you help them to find out Ai and Bi for all the
highways?
Input
First line of the input contains a positive integer T (T < 25), denoting the number of test cases.
First line of each test contains two integer numbers N and M (1 ≤ N ≤ 10000, N − 1 ≤ M ≤
100000), denoting the number of city and number of highway respectively. Each of the next M lines
contains the description of a highway, where the i-th line contains three integer numbers Ui
, Vi and Ci
(1 ≤ U i, V i ≤ N, U i ̸= V i, 0 ≤ Ci ≤ 1000), that means there is a highway between city Ui and city
Vi and the toll of the highway is Ci
. You may consider the highways to be bidirectional. Note that
the first N − 1 highways in the input are the special highways. You may assume that there will be no
invalid data in the input file.
Output
For each test case, output the test case number and a single integer S, where
S =

M
i=1
(i ∗ Ai + i
2
∗ Bi)

题解:

其实是一道很裸的题目. 唯一需要想的是,对于树边,它增大的时候需要考虑所有跨过它的非树边,那么反着想,就是所有非树边更新它这条链上的所有树边.之后就是边的树链剖分.

重点:

1.树边获取和用非树边更新的关系
2.边的树链剖分

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>using namespace std;typedef long long ll;
const int maxn = 10000 + 100;
const int MAXNODE = 4 * maxn + 100;
const int M = 10000;struct edge {int to, w;edge(int _to = 0, int _w = 0) {to = _to;w = _w;}
};
vector<edge> G[maxn];
int n, deep[maxn], m;
int tp[maxn], fa[maxn], son[maxn], w[maxn], no[maxn], fanno[maxn], cnt;
int tree[MAXNODE], tag[MAXNODE];
int A[maxn * 10], B[maxn * 10];
int tu[maxn * 10], tv[maxn * 10], tw[maxn * 10];
int st[21][maxn], L2[maxn];void st_initail() {for(int s = 1; s <= 20; s++) {for(int i = 1;i + (1 << s) - 1 <= cnt; i++) {int j = i + (1 << (s - 1));st[s][i] = max(st[s - 1][i], st[s - 1][j]);}}
}
int st_query(int l, int r) {int len = (r - l + 1);int s = L2[len];return max(st[s][l], st[s][r - (1 << s) + 1]);
}void dfs1(int u, int pa) {w[u] = 1;son[u] = 0;for(int i = 0; i < G[u].size(); i++) {int v = G[u][i].to;if(v != pa) {deep[v] = deep[u] + 1;fa[v] = u;dfs1(v, u);w[u] += w[v];if(w[v] > w[son[u]])son[u] = v;}}
}
void dfs2(int u, int top) {tp[u] = top;cnt++;no[u] = cnt;fanno[cnt] = u;if(son[u] != 0) {dfs2(son[u], top);}for(int i = 0; i < G[u].size(); i++) {int v = G[u][i].to;if(v != fa[u] && v != son[u]) {dfs2(v, v);}}
}void dfs3(int u) {for(int i = 0; i < G[u].size(); i++) {int v = G[u][i].to, w = G[u][i].w;if(v != fa[u]) {st[0][no[v]] = w;dfs3(v);}}}void initail(int rt, int l, int r) {//printf("aaa  %d %d %d\n", rt, l, r);tree[rt] = M;tag[rt] = M;if(l == r) {;}else {int mid = (l + r) / 2;initail(rt * 2, l, mid);initail(rt * 2 + 1, mid +1, r);}
}void pushDown(int rt) {if(tag[rt] == M)return;int lrt = 2 * rt, rrt = 2 * rt + 1;tag[lrt] = min(tag[lrt], tag[rt]);tag[rrt] = min(tag[rrt], tag[rt]);tree[lrt] = min(tree[lrt], tag[rt]);tree[rrt] = min(tree[rrt], tag[rt]);tag[rt] = M;
}
void pushUp(int rt) {int lrt = 2 * rt, rrt = 2 * rt + 1;tree[rt] = min(tree[lrt], tree[rrt]);
}void update(int L, int R, int k, int rt, int l, int r) {//printf("ccc  %d %d %d\n", rt, l, r);if(L <= l && R >= r) {tag[rt] = min(tag[rt], k);tree[rt] = min(tree[rt], k);return;}pushDown(rt);int mid = (l + r) / 2, lrt = 2 * rt, rrt = lrt + 1;if(L <= mid) {update(L, R, k, lrt, l, mid);}if(R >= mid + 1) {update(L, R, k, rrt, mid + 1, r);}pushUp(rt);
}
int query(int pos, int rt, int l, int r) {if(l == r) {return tree[rt];}pushDown(rt);int mid = (l + r) / 2, lrt = 2 * rt, rrt = 2 * rt + 1;if(pos <= mid)return query(pos, lrt, l, mid);return query(pos, rrt, mid + 1, r);
}void change(int u, int v, int k) {int tpu = tp[u], tpv = tp[v];while(tpu != tpv) {if(deep[tpu] < deep[tpv]) {swap(tpu, tpv);swap(u, v);}update(no[tpu], no[u], k, 1, 1, cnt);u = fa[tpu];tpu = tp[u];}if(u == v)return;if(deep[u] < deep[v]) {swap(u, v);}//printf("bbb %d %d %d\n", no[son[v]], no[u], k);update(no[son[v]], no[u], k, 1, 1, cnt);
}int queryMax(int u, int v) {int ans = 0;int tpu = tp[u], tpv = tp[v];while(tpu != tpv) {if(deep[tpu] < deep[tpv]) {swap(tpu, tpv);swap(u, v);}ans = max(ans, st_query(no[tpu], no[u]));u = fa[tpu];tpu = tp[u];}if(u == v)return ans;if(deep[u] < deep[v])swap(u, v);// printf("888 %d  %d\n", u, v);ans = max(ans, st_query(no[son[v]], no[u]));return ans;}void solve() {memset(st[0], 0, sizeof(st[0]));memset(deep, 0, sizeof(deep));memset(w, 0, sizeof(w));dfs1(1, 0);//printf("%d %d %d\n", getLCA(2, 3), getLCA(2, 3), getLCA(1, 3));tp[1] = 1;cnt = 0;dfs2(1, 1);initail(1, 1, cnt);//printf("%d\n", cnt);dfs3(1);st_initail();//printf("777 %d %d\n", queryMax(1, 3), no[2]);for(int i = 0; i < m - (n - 1); i++) {int u, v, c;scanf("%d %d %d", &u, &v, &c);A[n - 1 + i] = -1;B[n - 1 + i] = c - queryMax(u, v);//printf("666 %d\n", B[n - 1 + i]);change(u, v, c);}for(int i = 0; i < n - 1; i++) {B[i] = -1;int t = tu[i];if(deep[tu[i]] > deep[tv[i]])t = tu[i];elset = tv[i];A[i] = query(no[t], 1, 1, cnt);if(A[i] == M) {A[i] = -1;}else {A[i] = A[i] - tw[i];}}ll ans = 0;for(int i = 0; i < m; i++) {//printf("ddd   %d  %d  %d\n", i, A[i], B[i]);ans = ans + (ll)(i + 1) * (ll)A[i] + (ll)(i + 1) * (ll)(i + 1) * (ll)B[i];}printf("%lld\n", ans);
}int main() {//freopen("c.txt", "r", stdin);L2[0] = -1;for(int i = 1; i <= 10000; i++) {if((i & (i - 1)) == 0)L2[i] = L2[i - 1] + 1;elseL2[i] = L2[i - 1];}int ncase;scanf("%d", &ncase);for(int _ = 1; _ <= ncase; _++) {printf("Case %d: ", _);scanf("%d%d", &n, &m);for(int i = 1; i <= n; i++)G[i].clear();for(int i = 0; i < n - 1; i++) {int u, v, c;scanf("%d%d%d", &u, &v, &c);G[u].push_back(edge(v, c));G[v].push_back(edge(u, c));tu[i] = u;tv[i] = v;tw[i] = c;}solve();}return 0;
}

UVALive 7338 C - Toll Management IV相关推荐

  1. Regional 做题记录 (50/50)

    写在前面 博主深感自己太弱了QAQ 于是有了一个刷水的想法,Regional的题目还是有很多考查思维的题目,所以这次是乱做50道思考题,可能会顺带做一些水题,这些题的简要题解会写到这篇博文里面,希望能 ...

  2. .Net Core 3.1实现微信公众号发送模板消息,且跳转微信小程序

    .Net Core 3.1开发微信相关 背景 微信公众号和小程序关联 微信小程序获取UnionId及OpenId 微信公众号获取UnionId及OpenId 背景 公司需要将内部产品的审批放到微信小程 ...

  3. UVALive 2238 Fixed Partition Memory Management 固定分区内存管理(KM算法,变形)

    题意:目前有一部分可用内存,分为m个大小固定的区域.现有n个程序要执行,每个程序在不同大小的内存中运行所需的时间不同,要使运行完所有程序所耗时最少,问每个程序在哪块区域中从什么时间运行到什么时间,以及 ...

  4. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第IV部分

    sql还原数据库备份数据库 In this article, we'll see the how the backup-and-restore meta-data tables store the i ...

  5. 和慧眼显示服务器网关错误,R680G7联想万全慧眼IV高级版用户指南2.pdf

    目 录 第一章 简介 1 1.1 术语 1 1.2 安全信息 2 第二章 联想万全慧眼远程管理模块概述 4 2.1 联想万全慧眼高级版特性 4 2.2 支持的操作系统 5 2.2.1 服务器系统 5 ...

  6. DP UVALive 6506 Padovan Sequence

    题目传送门 /*题意:两行数字,相邻列一上一下,或者隔一列两行都可以,从左到右选择数字使和最大DP:状态转移方程:dp[i][j] = max (dp[i][j], dp[1-i][j-1] + a[ ...

  7. (札记)Altera Stratix IV系列FPGA TRUE LVDS RX input termination 在Quartus工程中的设置方法...

    Altera Stratix IV系列FPGA Row bank的TRUE LVDS_RX支持oct(on chip termination),所以设计的时候不需要外接一个100ohm电阻.备注:我使 ...

  8. Provisioning Services 7.8 入门系列教程之十三 使用 Boot Device Management(BDM)

    续Provisioning Services 7.8 入门系列教程之十二 实现高可用性 可以使用 Boot Device Management 实用程序将 IP 和引导信息(引导设备)交付给目标设备, ...

  9. 用XCA(X Certificate and key management)可视化程序管理SSL 证书(3)--创建自己定义的凭证管理中心(Certificate Authority)...

    在第"用XCA(X Certificate and key management)可视化程序管理SSL 证书(2)---创建证书请求"章节中,我们介绍了怎样用XCA创建SSL证书请 ...

最新文章

  1. Python 标准库之 Queue
  2. 优秀领导者,离不开这6个品质
  3. 添加时间数据时 出现多一天或者少一天的问题 解决方案
  4. matlab 正四面体,填隙工具箱,caulking box,音标,读音,翻译,英文例句,英语词典
  5. [转帖]Ubuntu 对应内核版本
  6. LeetCode 1705. 吃苹果的最大数目(优先队列)
  7. LeetCode MySQL 1280. 学生们参加各科测试的次数
  8. C++每日练笔之时间类(基类)
  9. Linux之特殊的环境变量IFS以及如何删除带有空格的目录
  10. [转载] python中numpy包使用方法总结
  11. 关于updatepanel回传之后JS失效问题
  12. WordPress采集插件-WordPress文章自动采集发布
  13. 自动生成光纤测试资料的软件,光缆测试模板生成工具官方版
  14. 坚守,一个烂俗的词,驱动人生带它走过了15年
  15. 【CSS】选择器汇总
  16. 让AI做作业:基于PaddleNLP-Taskflow的错别字单项测试
  17. libc_database 库文件下载
  18. MVCC及其原理详解
  19. 5G时代AI成城市交通管理的标配,谁会成为大赢家?
  20. css中min-height和max-height的区别

热门文章

  1. 公司注册资金到底多少才是最好
  2. 最新劳动强度排行榜 公务员排名绝对第一
  3. 通过 order_id 获取 参与拼团的 人 的头像
  4. KeePass 2.37版本的简体中文翻译更新
  5. Atlassian代码评审Fisheye and Crucible中代码库编码格式修改
  6. Google voice充值
  7. vivox21升级鸿蒙,vivo X21刷机教程_vivo X21A卡刷升级更新官方系统包
  8. druid 大量sleep连接
  9. 文件批量改名和批量替换文件内容
  10. 讲真的,我刚开始工作的时候,也很迷茫