COJ500 杨老师的路径规划(MST) (我是认真的)
用LCT来维护生成树,动态加边(s,t,w)时,新建节点x,权值为边权w。
1.若s与t不连通,则连接s-x,x-t,答案+w
2.若s与t连通,找出s-t路径上的最大权w2,若w<w2,删除w2的连边,连接s-x,x-t,答案+w-w2
太水了是不是!
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define lc ch[x][0]
#define rc ch[x][1]
using namespace std;
inline int read()
{
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
int ch[maxn][],fa[maxn],pre[maxn],flip[maxn],mx[maxn],v[maxn];
void maintain(int x)
{
mx[x]=x;
if(v[mx[lc]]>v[mx[x]]) mx[x]=mx[lc];
if(v[mx[rc]]>v[mx[x]]) mx[x]=mx[rc];
}
void pushdown(int x)
{
if(!flip[x]) return;
swap(lc,rc);flip[lc]^=;flip[rc]^=;flip[x]=;
}
void rotate(int x)
{
int y=pre[x],z=pre[y],d=ch[y][]==x;
ch[y][d^]=ch[x][d];pre[ch[x][d]]=y;
ch[z][ch[z][]==y]=x;pre[x]=z;
ch[x][d]=y;pre[y]=x;maintain(y);
}
int q[maxn],top;
void splay(int x)
{
for(int i=x;i;i=pre[i]) q[++top]=i;
if(top!=) fa[x]=fa[q[top]];
while(top) pushdown(q[top--]);
while(pre[x]) rotate(x);
maintain(x);
}
void access(int x)
{
for(int y=;x;x=fa[x])
{
splay(x);pre[ch[x][]]=;fa[ch[x][]]=x;
ch[x][]=y;pre[y]=x;
maintain(y=x);
}
}
void makeroot(int x) {access(x);splay(x);flip[x]^=;}
void link(int x,int y) {makeroot(x);fa[x]=y;}
void cut(int x,int y) {makeroot(x);access(y);splay(y);ch[y][]=pre[ch[y][]]=;maintain(x);}
int find(int x) {access(x);splay(x);while(ch[x][]) x=ch[x][];return x;}
int query(int x,int y)
{
makeroot(x);access(y);splay(y);return mx[y];
}
int s[maxn],t[maxn];
int main()
{
int n=read(),m=n*(n-)>>,ToT=n,ret=;
for(int i=;i<=m;i++)
{
s[i]=read(),t[i]=read(),v[++ToT]=read();
if(find(s[i])!=find(t[i])) link(s[i],ToT),link(ToT,t[i]),ret+=v[ToT];
else
{
int p=query(s[i],t[i]);if(v[p]>v[ToT])
{
ret+=v[ToT]-v[p];
cut(p,s[p-n]);cut(p,t[p-n]);
link(s[i],ToT);link(ToT,t[i]);
}
}
}
printf("%d\n",ret);
return ;
}
COJ500 杨老师的路径规划(MST) (我是认真的)的更多相关文章
- COJ 0500 杨老师的路径规划(MST)最小生成树
杨老师的路径规划(MST) 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 为满足同学们需求,杨老师在实验楼4层新建了好多个计算 ...
- 第四届58topcoder编程大赛--地图路径规划
layout: post title: 第四届58topcoder编程大赛 subtitle: 58ACM catalog: true tags: - A* 算法 - C++ - 程序设计 问题及背景 ...
- 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa
3627: [JLOI2014]路径规划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 186 Solved: 70[Submit][Status] ...
- 基于谷歌地图的Dijkstra算法水路路径规划
最终效果图如下: 还是图.邻接表,可以模拟出几个对象=>节点.边.路径.三个类分别如下: Node 节点: using System; using System.Collections.Gene ...
- Unity路径规划
Unity路径规划 转自:http://www.cnblogs.com/zsb517/p/4090629.html 背景 酷跑游戏中涉及到弯道.不规则道路. 找来一些酷跑游戏的案例来看,很多都是只有 ...
- iOS百度地图路径规划和POI检索详细总结-b
路径规划.png 百度地图的使用 百度地图API的导入网上说了许多坑,不过我遇到的比较少,这里就放两个比较常见的吧.坑一: 奥联WIFI_xcodeproj.png 如上图所示,在infoplist里 ...
- octomap中3d-rrt路径规划
路径规划 碰撞冲突检测 在octomap中制定起止点,目标点,使用rrt规划一条路径出来,没有运动学,动力学的限制,只要能避开障碍物. 效果如下: #include "ros/ros.h&q ...
- ROS(indigo)RRT路径规划
源码地址:https://github.com/nalin1096/path_planning 路径规划 使用ROS实现了基于RRT路径规划算法. 发行版 - indigo 算法在有一个障碍的环境找到 ...
- ROS探索总结(十四)——move_base(路径规划)
在上一篇的博客中,我们一起学习了ROS定位于导航的总体框架,这一篇我们主要研究其中最重要的move_base包. 在总体框架图中可以看到,move_base提供了ROS导航的配置.运行.交互接口,它主 ...
随机推荐
- qcow2文件压缩
qemu-img convert -O qcow2 /path/old.img.qcow2 /path/new.img.qcow2 转自:https://havee.me/linux/2011-09/ ...
- Resumable uploads over HTTP. Protocol specification
Valery Kholodkov <valery@grid.net.ru>, 2010 1. Introduction This document describes applicatio ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- iOS 关于Layer的疑问
很久很久以前,就对ios的Layer十分的不解,学习了android后,打算通过android中的相关实现,分析一下ios中layer的作用,结果没有找到android中的对应的内容!十分让人郁闷.于 ...
- 桐桐的贸易--WA
问题 A: 桐桐的贸易 时间限制: 1 Sec 内存限制: 64 MB提交: 15 解决: 2[提交][状态][讨论版] 题目描述 桐桐家在Allianceance城,好友ROBIN家在Horde ...
- JavaScript设计模式 - 迭代器模式
迭代器模式是指提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示. 迭代器模式可以把迭代的过程从业务逻辑中分离出来,在使用迭代器模式之后,即使不关心对象的内部构造,也可以按顺 ...
- [Android Pro] Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题
参考博客: http://blog.csdn.net/t12x3456/article/details/9256609 http://blog.csdn.net/lihenair/article/de ...
- Step deep into GLSL
1 Lighting computation is handled in eye space(需要根据眼睛的位置来计算镜面发射值有多少进入眼睛), hence, when using GLSL (GP ...
- php 克隆和引用类
/*class Ren { public $name; public $sex; function __construct($n,$s) { $this->name=$n; $this-> ...
- Spring中的设计模式学习
Spring提供了一种Template的设计哲学,包含了很多优秀的软件工程思想. 1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. ...