Problem 2082 过路费

Accept: 382    Submit: 1279

Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

有n座城市,由n-1条路相连通,使得随意两座城市之间可达。每条路有过路费,要交过路费才干通过。每条路的过路费常常会更新,现问你,当前情况下,从城市a到城市b最少要花多少过路费。

Input

有多组例子,每组例子第一行输入两个正整数n,m(2 <= n<=50000,1<=m <= 50000),接下来n-1行,每行3个正整数a b c,(1 <= a,b <= n , a != b , 1 <= c <= 1000000000).数据保证给的路使得随意两座城市互相可达。接下来输入m行,表示m个操作,操作有两种:一. 0 a b,表示更新第a条路的过路费为b,1 <= a <= n-1 ; 二. 1 a b , 表示询问a到b最少要花多少过路费。

Output

对于每一个询问。输出一行,表示最少要花的过路费。

Sample Input

2 31 2 11 1 20 1 21 2 1

Sample Output

12

Source

FOJ有奖月赛-2012年4月(校赛热身赛)

ac代码

RunID: 628735
UserID: kxh1995_joker
Submit time: 2015-08-17 18:05:24
Language: C++
Length: 2884 Bytes.
Result: Accepted
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<iostream>
using namespace std;
int head[50050],cnt,vis[50050];
struct s
{
int u,v,w,next;
}edge[50050<<1];
struct LCT
{
int bef[50050],pre[50050],next[50050][2],key[50050],sum[50050],belong[50050];
void init()
{
memset(pre,0,sizeof(pre));
memset(next,0,sizeof(next));
}
void pushup(int x)
{
sum[x]=key[x]+sum[next[x][1]]+sum[next[x][0]];
}
void rotate(int x,int kind)
{
int y,z;
y=pre[x];
z=pre[y];
next[y][!kind]=next[x][kind];
pre[next[x][kind]]=y;
next[z][next[z][1]==y]=x;
pre[x]=z;
next[x][kind]=y;
pre[y]=x;
pushup(y);
}
void splay(int x)
{
int rt;
for(rt=x;pre[rt];rt=pre[rt]);
if(x!=rt)
{
bef[x]=bef[rt];
bef[rt]=0;
while(pre[x])
{
if(next[pre[x]][0]==x)
{
rotate(x,1);
}
else
rotate(x,0);
}
pushup(x);
}
}
void access(int x)
{
int fa;
for(fa=0;x;x=bef[x])
{
splay(x);
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=fa;
pre[fa]=x;
bef[fa]=0;
fa=x;
pushup(x);
}
}
void change(int x,int val)
{
int t;
t=belong[x-1];
key[t]=val;
splay(t);
}
int query(int x,int y)
{
access(y);
for(y=0;x;x=bef[x])
{
splay(x);
if(!bef[x])
return sum[y]+sum[next[x][1]];
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=y;
pre[y]=x;
bef[y]=0;
y=x;
pushup(x);
}
}
}lct;
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void bfs(int u)
{
int i,y;
queue<int>q;
memset(vis,0,sizeof(vis));
vis[u]=1;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
lct.bef[v]=u;
lct.key[v]=lct.sum[v]=edge[i].w;
lct.belong[i>>1]=v;
vis[v]=1;
q.push(v);
}
}
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
lct.init();
bfs(1);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==0)
{
lct.change(b,c);
}
else
printf("%d\n",lct.query(b,c));
}
}
}

FOJ题目Problem 2082 过路费 (link cut tree边权更新)的更多相关文章

  1. HDOJ 题目2475 Box(link cut tree去点找祖先)

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. Fzu Problem 2082 过路费 LCT,动态树

    题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528    Submit: 1654Time Limit ...

  3. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  4. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  5. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  6. B - Link/Cut Tree

    Problem description Programmer Rostislav got seriously interested in the Link/Cut Tree data structur ...

  7. FZU Problem 2082 过路费 树链剖分

    Problem 2082 过路费    Problem Description 有n座城市,由n-1条路相连通,使得任意两座城市之间可达.每条路有过路费,要交过路费才能通过.每条路的过路费经常会更新, ...

  8. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  9. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

随机推荐

  1. MySql查询系统时间,SQLServer查询系统时间,Oracle查询系统时间

    转自:https://blog.csdn.net/haleyliu123/article/details/70927668/ MySQL查询系统时间 第一种方法:select current_date ...

  2. 基于mkdocs-material搭建个人静态博客

    基于mkdocs-material搭建个人纯静态博客,没有php,没有mysql 如果你只是想安安静静的放一些技术文章,发布到个人站点或github-pages,mkdocs-material很适合你 ...

  3. 在linux上加速git clone

    在linux上加速git clone 进入终端命令行模式,sudo vim /etc/hosts 编辑hosts文件,添加以下ip-域名,保存退出 151.101.44.249 github.glob ...

  4. Hadoop MapReduce编程 API入门系列之MapReduce多种输出格式分析(十九)

    不多说,直接上代码. 假如这里有一份邮箱数据文件,我们期望统计邮箱出现次数并按照邮箱的类别,将这些邮箱分别输出到不同文件路径下. 代码版本1 package zhouls.bigdata.myMapR ...

  5. Hadoop MapReduce编程 API入门系列之多个Job迭代式MapReduce运行(十二)

    推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...

  6. DevExpress 如何读取当前目录下文件,加载至grid

    DBFileName=DevExpress.Utils.FileHelper.FindingFileName(Appliaction.StartupPath,"Data\\Product&g ...

  7. 2nd

    Java语言基础(常量的概述和使用) A:什么是常量 在程序执行的过程中其值不可以发生改变 B:Java中常量的分类 字面值常量 自定义常量(面向对象部分讲) C:字面值常量的分类 字符串常量 用双引 ...

  8. 杭电2061WA 01

    #include<stdio.h> struct mem { char s[50]; double c; double f; }; int main() { struct mem x[60 ...

  9. tomcat 使用常见问题

    tomcat 下目录的介绍: 参考地址: http://blog.csdn.net/u013132035/article/details/54949593 参考书籍:tomcat权威指南 (1) 非w ...

  10. java 常用API 包装 练习

    package com.oracel.demo01; import java.util.Random; public class Swzy { public static void main(Stri ...