Traffic Network in Numazu

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 677    Accepted Submission(s): 269

Problem Description

Chika is elected mayor of Numazu. She needs to manage the traffic in this city. To manage the traffic is too hard for her. So she needs your help. 

You are given the map of the city —— an undirected connected weighted graph with N nodes and N edges, and you have to finish Q missions. Each mission consists of 3 integers OP, X and Y. 

When OP=0, you need to modify the weight of the Xth edge to Y. 

When OP=1, you need to calculate the length of the shortest path from node X to node Y.

Input

The first line contains a single integer T, the number of test cases. 

Each test case starts with a line containing two integers N and Q, the number of nodes (and edges) and the number of queries. (3≤N≤105)(1≤Q≤105) 

Each of the following N lines contain the description of the edges. The ith line represents the ith edge, which contains 3 space-separated integers ui, vi, and wi. This means that there is an undirected edge between nodes ui and vi, with a weight of wi. (1≤ui,vi≤N)(1≤wi≤105) 

Then Q lines follow, the ith line contains 3 integers OP, X and Y. The meaning has been described above.(0≤OP≤1)(1≤X≤105)(1≤Y≤105) 

It is guaranteed that the graph contains no self loops or multiple edges.

Output

For each test case, and for each mission whose OP=1, print one line containing one integer, the length of the shortest path between X and Y.

Sample Input


 

2 5 5 1 2 3 2 3 5 2 4 5 2 5 1 4 3 3 0 1 5 1 3 2 1 5 4 0 5 4 1 5 1 5 3 1 2 3 1 3 2 3 4 4 4 5 5 2 5 5 0 1 3 0 4 1 1 1 4

Sample Output


 

5 6 6 6

Source

2018 Multi-University Training Contest 7

Recommend

chendu   |   We have carefully selected several similar problems for you:  6408 6407 6406 6405 6404

看题目就觉得是会是一道线段树单点修改区间查询

但是题目是一个图 n个点n条边

于是czc提出去掉一条边 看了题解果然是这样子

  • 先把环中的任意一条边去除,边连接的两点记为r1,r2
  • 每次查询的时候,x与y之间的距离有三个:(1)去除边后树上的最短距离;(2)x->r1->r2->y;(3) x->r2->r1->y。取三个值中的最小值即可

树状数组和线段树是非常相近的 今天去查了一下区别

树状数组可以解决的题目线段树都一定能解决,但是反之不一定

也就是说线段树的功能更强大一点

但是树状数组写起来比较方便,而且常数比线段树好,速度快

这道题因为是问区间和 所以树状数组够用了

这道题还有一个知识点是LCA

树上的最短路可以用LCA来求 Tarjan算法 =  dfs序+并查集的find


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std; int t, n, q;
const int maxn = 100050;
struct edge{
int v, nxt, val;
}e[maxn * 2];
int head[maxn], tot; void addedge(int x, int y, int d)
{
e[++tot] = (edge){y, head[x], d};
head[x] = tot;
} long long sum[maxn];
void add(int x, int val)
{
while(x <= n){
sum[x] += val;
x += (x & -x);
}
} long long query(int x)
{
long long ans = 0;
while(x){
ans += sum[x];
x -= (x & -x);
}
return ans;
} int fa[maxn];
int find(int x)
{
if(x == fa[x]){
return x;
}
else
return fa[x] = find(fa[x]);
} int idx[maxn], cnt;//dfs序
int dep[maxn], f[maxn][20], r[maxn]; void dfs(int x, int par, int val)
{
fa[x] = f[x][0] = par;
dep[x] = dep[par] + 1;
idx[x] = ++cnt;
add(cnt, val);
for(int i = 1; f[x][i - 1]; ++i){
f[x][i] = f[f[x][i - 1]][i - 1];
}
for(int i = head[x]; i; i = e[i].nxt){
if(par != e[i].v)
dfs(e[i].v, x, e[i].val);
}
r[x] = cnt;
add(cnt + 1, -val);
} int lca(int x, int y)
{
if(dep[x] < dep[y])swap(x, y);
int h = dep[x] - dep[y];
for(int i = 19; i >= 0; --i){
if(h & (1 << i))
x = f[x][i];
}
if(x == y)return x;
for(int i = 19; i >= 0; --i){
if(f[x][i] != f[y][i]){
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
} long long dis(int x, int y)
{
int z = lca(x, y);
return query(idx[x]) + query(idx[y]) - query(idx[z]) * 2;
} int X[maxn], Y[maxn], W[maxn];
int main()
{
cin>>t;
while(t--){
scanf("%d%d", &n, &q);
for(int i = 0; i <= n; i++){
head[i] = 0;
fa[i] = i;
sum[i] = 0;
}
cnt = 0;
tot = 0;
int r1 = 0, r2 = 0, bw = 0, id;
for(int i = 1; i <= n; i++){
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
if(find(x) == find(y)){
r1 = x;
r2 = y;
bw = w;
id = i;
}
else{
addedge(x, y, w);
addedge(y, x, w);
fa[find(x)] = find(y);
}
X[i] = x;
Y[i] = y;
W[i] = w;
}
dfs(1, 0, 0);
while(q--){
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if(op == 0){
if(id == x)bw = y;
else{
int u = (fa[X[x]] == Y[x]?X[x]:Y[x]);
add(idx[u], -W[x] + y);
add(r[u] + 1, W[x] - y);
W[x] = y;
}
}
else{
long long ans = dis(x, y);
ans = min(ans, dis(r1, x) + dis(r2, y) + bw);
ans = min(ans, dis(r1, y) + dis(r2, x) + bw);
printf("%lld\n", ans);
}
}
}
return 0;
}

hdu6393Traffic Network in Numazu【树状数组】【LCA】的更多相关文章

  1. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  2. HDU4836 The Query on the Tree(树状数组&&LCA)

    由于智力的问题,百度之星完全lu不动..开场看第一题根据题目给的条件我觉得一定是可以构造出来的,题目给的意思颇有鸽巢原理的感觉,于是觉得开场第一题应该就是智力构造题了,想了半个小时,发现完全想不动,于 ...

  3. BZOJ3881[Coci2015]Divljak——AC自动机+树状数组+LCA+dfs序+树链的并

    题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

  4. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  5. 【BZOJ2819】Nim 树状数组+LCA

    [BZOJ2819]Nim Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可 ...

  6. 【BZOJ4999】This Problem Is Too Simple! 离线+树状数组+LCA

    [BZOJ4999]This Problem Is Too Simple! Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2 ...

  7. [CSP-S模拟测试]:异或(树状数组+LCA)

    题目传送门(内部题21) 输入格式 第一行一个字符串$str$,表示数据类型.第二行一个正整数$k$,表示集合$K$的大小,保证$k>1$.接下来$k$行每行$k$个数,第$i$行第$j$个数表 ...

  8. POJ 2763"Housewife Wind"(DFS序+树状数组+LCA)

    传送门 •题意 一对夫妇居住在 xx村庄,给村庄有 $n$ 个小屋: 这 $n$ 个小屋之间有双向可达的道路,不会出现环,即所构成的图是个树: 从 $a_i$ 小屋到 $b_i$ 小屋需要花费 $w_ ...

  9. Codeforces 1111E DP + 树状数组 + LCA + dfs序

    题意:给你一颗树,有q次询问,每次询问给你若干个点,这些点可以最多分出m组,每组要满足两个条件:1:每组至少一个点,2:组内的点不能是组内其它点的祖先,问这样的分组能有多少个? 思路:https:// ...

  10. BZOJ 2819: Nim( nim + DFS序 + 树状数组 + LCA )

    虽然vfleaking好像想卡DFS...但我还是用DFS过了... 路径上的石堆异或和=0就是必败, 否则就是必胜(nim游戏). 这样就变成一个经典问题了, 用DFS序+BIT+LCA就可以在O( ...

随机推荐

  1. Linux oracle数据库创建表空间、用户并赋予权限

    管理员用户登录oracle数据库 1.创建临时表空间 select name from v$tempfile;查出当前数据库临时表空间,主要是使用里面的存放路径: 得到其中一条记录/opt/oracl ...

  2. Python中的yield和Generators(生成器)

    本文目的 解释yield关键字到底是什么,为什么它是有用的,以及如何来使用它. 协程与子例程 我们调用一个普通的Python函数时,一般是从函数的第一行代码开始执行,结束于return语句.异常或者函 ...

  3. PHP代码审计笔记--弱类型存在的安全问题

    0x01 前言 PHP 是一门弱类型语言,不必向 PHP 声明该变量的数据类型,PHP 会根据变量的值,自动把变量转换为正确的数据类型. 弱类型比较,是一个比较蛋疼的问题,如左侧为字符串,右侧为一个整 ...

  4. SpringBoot(五)-- 整合Spring的拦截器

    一.步骤 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口. 2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptor ...

  5. Memcached 命令行操作

    telnet 用于连接 Memcached: [root@localhost ~]# telnet Trying 127.0.0.1... Connected to 127.0.0.1. Escape ...

  6. linux sumba服务器简单配置

    使用samba设置linux和windows直接简单的文件共享 前提: 1.linux和windows已经可以互相ping同 2.已经安装好smb 查看是否安装smb rpm -aq|grep smb ...

  7. Sqlserver 数据库、表常用查询操作

    查询所有表以及记录数: select a.name as 表名,max(b.rows) as 记录条数 from sysobjects a ,sysindexes b where a.id=b.id ...

  8. DELPHIER,正转向PYTHON了

    用了10多年的DELPHI ,现在准备学PYTHON了, 理由是: 1)现在是云计算时代了,需要学习一门适应该时代的语言,研究了很久,PYTHON是最佳选择,排除了PHP,PERL,RUBY等,至于G ...

  9. scanf printf sprintf fprintf

    都是C语言中的函数,但C++保留了这些函数,在头文件iostream中声明了. 1 scanf(格式控制,输出列表) printf (格式控制,输出列表) 举例: #include <iostr ...

  10. Delete触发器