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导航的配置.运行.交互接口,它主 ...
随机推荐
- Reverse Pairs
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...
- IDEA 14快捷键
1.ctrl+alt+左箭头.右箭头:返回到上次浏览的代码处(相当于Eclipse的alt+左右箭头) 编辑类: Ctrl+Space 基本代码实例(类.方法.变量) Ctrl + Shift + S ...
- 【转】利用mybatis-generator自动生成代码
本文转自:http://www.cnblogs.com/yjmyzz/p/4210554.html mybatis-generator有三种用法:命令行.eclipse插件.maven插件.个人觉得m ...
- Java for LeetCode 045 Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- Oracle的锁表与解锁
Oracle的锁表与解锁 SELECT /*+ rule */ s.username, decode(l.type,'TM','TABLE LOCK', 'TX','ROW LOCK', NULL) ...
- SRAM的读写操作
自己写的SRAM的程序,主要在于实用性,适应工作的工程需要.使用芯片为: 芯片时序图为: 代码: /********************************Copyright********* ...
- php 字符串处理
<?php $a = " n001|n002|n003|n004 "; //echo strlen($a);//取字符串的长度 //var_dump(strcmp(" ...
- angular语法:Controller As
这个东东我觉得很好哟. 数据可以在同一个页面的不同的controller之间自由穿梭... 当然, https://thinkster.io/a-better-way-to-learn-angular ...
- HDU 1796 How many integers can you find 容斥入门
How many integers can you find Problem Description Now you get a number N, and a M-integers set, y ...