题目描述

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。

输入输出样例

输入样例#1: 复制

4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3
输出样例#1: 复制

3
-1
3

说明

对于 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 货车运输 树链剖分的更多相关文章

  1. luogu题解P1967货车运输--树链剖分

    题目链接 https://www.luogu.org/problemnew/show/P1967 分析 NOIp的一道裸题,直接在最大生成树上剖分取最小值一下就完事了,非常好写,常数也比较小,然而题解 ...

  2. NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】

    NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...

  3. NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)

    Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...

  4. BZOJ_4326_[NOIP2015]_运输计划_(二分+LCA_树链剖分/Tarjan+差分)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=4326 给出一棵带有边权的树,以及一系列任务,任务是从树上的u点走到v点,代价为u到v路径上的权 ...

  5. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

  6. cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分

    2109. [NOIP 2015] 运输计划 ★★★☆   输入文件:transport.in   输出文件:transport.out   简单对比时间限制:3 s   内存限制:256 MB [题 ...

  7. bzoj 4326: NOIP2015 运输计划(二分+树链剖分)

    传送门 题解: 树链剖分快速求解任意两点间的路径的权值和: 然后,二分答案: 此题的难点是如何快速求解重合路径? 差分数组可以否??? 在此之前先介绍一下相关变量: int fa[maxn]; int ...

  8. JZYZOJ1454 NOIP2015 D2T3_运输计划 二分 差分数组 lca tarjan 树链剖分

    http://172.20.6.3/Problem_Show.asp?id=1454 从这道题我充分认识到我的脑子里好多水orz. 如果知道了这个要用二分和差分写,就没什么思考上的难点了(屁咧你写了一 ...

  9. NOIP2015 运输计划 - 二分 + 树链剖分 / (倍增 + 差分)

    BZOJ CodeVS Uoj 题目大意: 给一个n个点的边带权树,给定m条链,你可以选择树中的任意一条边,将它置为0,使得最长的链长最短. 题目分析: 最小化最大值,二分. 二分最短长度mid,将图 ...

随机推荐

  1. 3星|《CMO到底能干多久?》:CEO必须决定供公司需要哪类CMO

    CMO到底能干多久?(<哈佛商业评论>增刊) <哈佛商业评论>关于CMO的6篇文章.我认为第一篇<为何CMO宝座坐不长>写的最好,是典型的哈评风格:做过实际调查.有 ...

  2. 关闭会声会影2018提示UEIP.dll找不到指定模块

    最近有一些会声会影2018用户反映在关闭后弹出UEIP.dll错误,不知道该怎么办才好,针对这个问题,小编下面为大家介绍下解决方法. 原因分析 出现这个错误跟会声会影安装路径有中文字符是密切相关的,导 ...

  3. hdu - 6277,2018CCPC湖南全国邀请赛B题,找规律,贪心找最优.

    题意: 给出N个小时,分配这些小时去写若干份论文,若用1小时写一份论文,该论文会被引用A次,新写一篇论文的话,全面的论文会被新论文引用一次. 找最大的H,H是指存在H遍论文,而且这些论文各被引用大于H ...

  4. NO.2:自学python之路------变量类型、列表、字典

    引言 本周初步认识了库,并学习了Python中各种类型的变量和常用操作.并完成了较为完善的用户与商家购物界面设计. 正文 模块: Python有标准库和第三方库.第三方库需要安装才能使用.大量的库可以 ...

  5. Hexo博客 云服务器搭建

    下载nodejs: https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz 解压:tar zxv       解压后编译:   ...

  6. Action Required: Please provide your Tax Identity Information - Amazon Seller Tax Identity Collection

    Hello ***,   Your selling privileges have been suspended because we have not received required tax i ...

  7. PCAP文件格式分析(做抓包软件之必备)

    转载源:http://blog.csdn.net/anzijin/article/details/2008333 http://www.ebnd.cn/2009/09/07/file-format-a ...

  8. Linux中打开文件管理器的命令

    在Mac中,我们可以使用open命令,在终端打开指定目录下的文件管理器,在Linux中,同样可以使用类似的命令:nautilus.

  9. 直接抱过来dd大牛的《背包九讲》来做笔记

    P01: 01背包问题 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路 这是最 ...

  10. Java接口interface,匿名内部类

    接口 1.接口内部为 常量+公用的抽象方法.类必须实现接口中的所有方法 2.接口的语法格式:不写abstract会自动添加,可以继承多个接口 修饰符不能使private,protected [修饰符] ...