P1967 货车运输 树链剖分
题目描述
AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。
输入输出格式
输入格式:
第一行有两个用一个空格隔开的整数n,mn,m,表示 AA 国有nn 座城市和 mm 条道路。
接下来 mm行每行33个整数 x, y, zx,y,z,每两个整数之间用一个空格隔开,表示从 xx号城市到yy号城市有一条限重为 zz 的道路。注意: xx 不等于 yy,两座城市之间可能有多条道路 。
接下来一行有一个整数 q,表示有 q 辆货车需要运货。
接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市运输货物到 y 城市,注意: x 不等于 y 。
输出格式:
共有 qq 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1−1。
输入输出样例
说明
对于 30\%30%的数据,0 < n < 1,000,0 < m < 10,000,0 < q< 1,0000<n<1,000,0<m<10,000,0<q<1,000;
对于 60\%60%的数据,0 < n < 1,000,0 < m < 50,000,0 < q< 1,0000<n<1,000,0<m<50,000,0<q<1,000;
对于 100\%100%的数据,0 < n < 10,000,0 < m < 50,000,0 < q< 30,000,0 ≤ z ≤ 100,0000<n<10,000,0<m<50,000,0<q<30,000,0≤z≤100,000。
冷静思考 这就是求一个最大生成树 然后在这一颗树上面求路径最短
然后我就被坑了 发现其实并不一定是联通总是不能全过
后面加了这个才过的
for ( int i = 1 ; i <= n ; i++ ) {
if ( id[i] ) continue;
dfs1 ( i, 0, 1 );
dfs2 ( i, i );
}
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define rtl rt<<1
#define rtr rt<<1|1
#define mid ((l+r)>>1)
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf prLLf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("in.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1e9 + ;
const int maxn = 5e4;
const int INF = 0x7fffffff;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
int n, m, tot, head[maxn];
int tree[maxn << ], w1[maxn], rk[maxn];
int son[maxn], id[maxn], fa[maxn], dep[maxn], sz[maxn], top[maxn], cnt;
int father[maxn];
struct Edge {
int v, nxt, w;
} edge[maxn];
void init() {
tot = cnt = ;
mem ( head, - );
mem ( son, - );
}
void add ( int u, int v, int w ) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs1 ( int u, int f, int deep ) {
dep[u] = deep;
fa[u] = f;
sz[u] = ;
for ( int i = head[u]; ~i; i = edge[i].nxt ) {
int v = edge[i].v;
if ( v == f ) continue;
w1[v] = edge[i].w;
dfs1 ( v, u, deep + );
sz[u] += sz[v];
if ( sz[son[u]] < sz[v] ) son[u] = v;
}
}
void dfs2 ( int x, int topf ) {
id[x] = ++cnt;
rk[cnt] = x;
top[x] = topf ;
if ( son[x] == - ) return;
dfs2 ( son[x], topf );
for ( int i = head[x]; ~i; i = edge[i].nxt ) {
int y = edge[i].v;
if ( y == fa[x] || y == son[x] ) continue;
dfs2 ( y, y );
}
}
void build ( int l, int r, int rt ) {
if ( l == r ) {
tree[rt] = w1[rk[l]];
return ;
}
build ( lson );
build ( rson );
tree[rt] = min ( tree[rtl], tree[rtr] );
}
int quary ( int L, int R, int l, int r, int rt ) {
int ans = INF;
if ( L <= l && r <= R ) return tree[rt];
if ( L <= mid ) ans = min ( ans, quary ( L, R, lson ) );
if ( R > mid ) ans = min ( ans, quary ( L, R, rson ) );
return ans;
}
int qrange ( int x, int y ) {
int ans = INF;
while ( top[x] != top[y] ) {
if ( dep[top[x]] < dep[top[y]] ) swap ( x, y );
ans = min ( ans, quary ( id[top[x]], id[x], , n, ) ) ;
x = fa[top[x]];
}
if ( x != y ) {
if ( dep[x] > dep[y] ) swap ( x, y );
ans = min ( ans, quary ( id[x] + , id[y], , n, ) ) ;
}
return ans ;
}
struct node {
int u, v, w;
} qu[maxn];
int cmp ( node a, node b ) {
return a.w > b.w;
}
int Find ( int x ) {
return father[x] == x ? x : father[x] = Find ( father[x] );
}
int combine ( int x, int y ) {
int nx = Find ( x ), ny = Find ( y );
if ( nx != ny ) {
father[ny] = nx;
return ;
}
return ;
}
void krucal() {
int k = ;
for ( int i = ; i < m ; i++ ) {
if ( combine ( qu[i].u, qu[i].v ) ) {
k++;
add ( qu[i].u, qu[i].v, qu[i].w );
add ( qu[i].v, qu[i].u, qu[i].w );
if ( k == n - ) break;
}
}
}
int main() {
scanf ( "%d%d", &n, &m );
init();
for ( int i = ; i < m ; i++ ) scanf ( "%d%d%d", &qu[i].u, &qu[i].v, &qu[i].w );
for ( int i = ; i <= n ; i++ ) father[i] = i;
sort ( qu, qu + m, cmp );
krucal();
for ( int i = ; i <= n ; i++ ) {
if ( id[i] ) continue;
dfs1 ( i, , );
dfs2 ( i, i );
}
build ( , n, );
int q;
scanf ( "%d", &q );
while ( q-- ) {
int x, y;
scanf ( "%d%d", &x, &y );
if ( Find ( x ) == Find ( y ) ) printf ( "%d\n", qrange ( x, y ) );
else printf ( "-1\n" );
}
return ;
}
P1967 货车运输 树链剖分的更多相关文章
- luogu题解P1967货车运输--树链剖分
题目链接 https://www.luogu.org/problemnew/show/P1967 分析 NOIp的一道裸题,直接在最大生成树上剖分取最小值一下就完事了,非常好写,常数也比较小,然而题解 ...
- NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】
NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...
- NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)
Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...
- BZOJ_4326_[NOIP2015]_运输计划_(二分+LCA_树链剖分/Tarjan+差分)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=4326 给出一棵带有边权的树,以及一系列任务,任务是从树上的u点走到v点,代价为u到v路径上的权 ...
- Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...
- cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分
2109. [NOIP 2015] 运输计划 ★★★☆ 输入文件:transport.in 输出文件:transport.out 简单对比时间限制:3 s 内存限制:256 MB [题 ...
- bzoj 4326: NOIP2015 运输计划(二分+树链剖分)
传送门 题解: 树链剖分快速求解任意两点间的路径的权值和: 然后,二分答案: 此题的难点是如何快速求解重合路径? 差分数组可以否??? 在此之前先介绍一下相关变量: int fa[maxn]; int ...
- JZYZOJ1454 NOIP2015 D2T3_运输计划 二分 差分数组 lca tarjan 树链剖分
http://172.20.6.3/Problem_Show.asp?id=1454 从这道题我充分认识到我的脑子里好多水orz. 如果知道了这个要用二分和差分写,就没什么思考上的难点了(屁咧你写了一 ...
- NOIP2015 运输计划 - 二分 + 树链剖分 / (倍增 + 差分)
BZOJ CodeVS Uoj 题目大意: 给一个n个点的边带权树,给定m条链,你可以选择树中的任意一条边,将它置为0,使得最长的链长最短. 题目分析: 最小化最大值,二分. 二分最短长度mid,将图 ...
随机推荐
- STC 单片机ADC实现原理
模数转换器原理 数模转换器( analog to digitI converter,ADC),简称为A/D,ADC是链接模拟世界和数字世界的桥梁.它用于将连续的模拟信号转换为数字形式离散信号.典型的, ...
- 寻找bug
bug1:void不应有返回值. bug2:while(n--)没有条件终止循环. bug3:size和data没有定义 bug4:arr 是sz 在大于0的情况下创建的 一定部位bull 下面的 ...
- 1.12Linux下软件安装(学习过程)
实验介绍 介绍 Ubuntu 下软件安装的几种方式,及 apt,dpkg 工具的使用. 一.Linux 上的软件安装 通常 Linux 上的软件安装主要有三种方式: 在线安装 从磁盘安装deb软件包 ...
- 实现Spring管理struts的Action
struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean. 可以在struts2中配置: <struts> ...
- Qt下Doxygen使用
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt下Doxygen使用 本文地址:http://techieliang.com/20 ...
- git因commit的记录太大导致push失败解决方法
发现好像这个方法不好使.......~~!还是会失败 如果有人或者自己失误把不该同步的大文件如数据或日志或其他中间文件给commit了并且push了,然后你删掉了,但是其实他仍然在你的git记录中,你 ...
- 用到的C++标准库
std::set<type>, 模板写的平衡二叉树的集合容器, method: insert, count, std:map<int, string>, 映射和多重映射基于某一 ...
- libcurl底层调用逻辑
libcurl就不多介绍了,一个支持HTTP,FTP,SMTP等协议的网络库 只涉及multi部分,easy部分就不提了. 两个线程,一个负责添加HTTP请求,另一个轮询,负责处理每一个请求 Thre ...
- 【c】线性表
数据对象集:线性表是N(>=0)个元素构成的有序序列,a1,a2,a3.....a(N-1),aN,a(N+1) 线性表上的基本操作有: ⑴ 线性表初始化:Init_List(L)初始条件:表L ...
- 第142天:Size Marks下载安装和使用方法
Size Marks下载安装使用方法 一.下载安装 1.下载Size marks:链接: https://pan.baidu.com/s/1breyMf1 密码: fjsn 2. 复制 Size Ma ...