题目描述

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. 在GPT格式的硬盘上,使用EFI启动的方式,安装Win7 64位系统

    Win7 sp1 原装系统,用UltraISO(软碟通) 把U 盘制成Win7 安装的启动U盘 将bootmgfw.efi和shell.efi 加到已制好启动U盘的根目录,并在efi/boot/路径下 ...

  2. 使用Scrapy构建一个网络爬虫

    记得n年前项目需要一个灵活的爬虫工具,就组织了一个小团队用Java实现了一个爬虫框架,可以根据目标网站的结构.地址和需要的内容,做简单的配置开发,即可实现特定网站的爬虫功能.因为要考虑到各种特殊情形, ...

  3. WEB前端开发流程总结

    作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 WEB前端开发项目流程总结 1.新建项目 ...

  4. Flip the Bits(思维)

    You are given a positive integer n. Your task is to build a number m by flipping the minimum number ...

  5. eclipse连接SQL2008R2

    最近又开始写JAVA WEB了,想起连接数据库就麻烦,但是通过一天的努力我居然弄好了,很有成就感. 我用的是 SQL Server 2008 R2  +  eclipse 首先要成功的安装好SQL最终 ...

  6. mvc4 找到多个与名为“xx”的控制器匹配的类型

    asp.net mvc4 添加分区出现错误 找到多个与名为“home”的控制器匹配的类型 会出现如下错误”找到多个与名为“home”的控制器匹配的类型“ 在RouteConfig文件中添加命名空间可解 ...

  7. Hibernate:工作原理

    Hibernate的工作原理图如下所示:

  8. DB2 9.5 数据库分区管理及应用实践

    DB2 数据库分区是 DB2 企业版 DPF(Data Partitioning Feature)选件提供的,它主要用来为大规模数据处理.高并发数据访问提供支持.DB2 数据库分区采用 Share-n ...

  9. Splash广告界面

    在软件开始启动时都是会使用一个splashActivity实现联网判断和相关资源的加载,在一款网络软件上开始时的缓存加载和网络判断可以为用户节省不必要的流量开销. 使用handler延时启动下一个ac ...

  10. SQL Server 一些操作语句

    查询表结构---sp_help 表名 或 sp_columns  表名 删表 -------drop table 表名删表中所有的数据----------truncate table 表名根据条件删表 ...