3287 货车运输

2013年NOIP全国联赛提高组

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 查看运行结果
 
 
题目描述 Description

A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。

输入描述 Input Description

第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。
接下来 m 行每行 3 个整数 x、y、z,每两个整数之间用一个空格隔开,表示从 x 号城市到 y 号城市有一条限重为 z 的道路。注意:x 不等于 y,两座城市之间可能有多条道路。
接下来一行有一个整数 q,表示有 q 辆货车需要运货。
接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市运输货物到 y 城市,注意:x 不等于 y。

输出描述 Output Description

输出共有 q 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1。

样例输入 Sample Input

4 3 
1 2 4 
2 3 3 
3 1 1 
3
1 3 
1 4 
1 3

样例输出 Sample Output

3
-1
3

数据范围及提示 Data Size & Hint

对于 30%的数据,0 < n < 1,000,0 < m < 10,000,0 < q < 1,000; 
对于 60%的数据,0 < n < 1,000,0 < m < 50,000,0 < q < 1,000; 
对于 100%的数据,0 < n < 10,000,0 < m < 50,000,0 < q < 30,000,0 ≤ z ≤ 100,000。

分类标签 Tags 点此展开

思路:

  最大生成树+lca

来,上代码:

#include<cstdio>
#include<algorithm> using namespace std; struct node {
int from,to,dis,next;
};
struct node usee[];
struct node edge[];
int n,m,head[],num=;
int f[],bnum=,tail=;
int cur=,dfn[],tarjan_dfn=; int max(int a,int b){return a>b?a:b;} int min(int a,int b){return a<b?a:b;} void tarjan(int scre,int before)
{
tarjan_dfn++;
dfn[scre]=tarjan_dfn;
for(int i=head[scre];i!=;i=edge[i].next)
{
if(edge[i].to!=before)
{
tarjan(edge[i].to,scre);
}
}
} int tarjan_lca(int minn,int maxn)
{
int kop=minn,kol=maxn,kcc=1e18;
while(dfn[kol]>dfn[minn])
{
for(int i=head[kol];i!=;i=edge[i].next)
{
if(dfn[edge[i].to]<dfn[kol])
{
kol=edge[i].to;
kcc=min(kcc,edge[i].dis);
break;
}
}
}
while(dfn[kop]>dfn[kol])
{
for(int i=head[kop];i!=;i=edge[i].next)
{
if(dfn[edge[i].to]<dfn[kop])
{
kop=edge[i].to;
kcc=min(kcc,edge[i].dis);
break;
}
}
}
return kcc;
} int cmp(struct node a,struct node b){return a.dis>b.dis;} void edge_add(int from,int to,int dis)
{
num++;
edge[num].to=to;
edge[num].dis=dis;
edge[num].from=from;
edge[num].next=head[from];
head[from]=num;
} int find(int x)
{
if(x==f[x]) return f[x];
else return f[x]=find(f[x]);
} int qread()
{
int x=;char ch=getchar();
while(ch>''||ch<'') ch=getchar();
while(ch<=''&&ch>=''){x=x*+(int)(ch-'');ch=getchar();}
return x;
} int main()
{
n=qread(),m=qread();
for(int i=;i<n;i++)
{
f[i]=i;
bnum++;
usee[bnum].from=i;
usee[bnum].to=i+;
usee[bnum].dis=-;
}
f[n]=n;
int from,to,dis;
for(int i=;i<=m;i++)
{
from=qread(),to=qread(),dis=qread();
bnum++;
usee[bnum].to=to;
usee[bnum].dis=dis;
usee[bnum].from=from;
}
sort(usee+,usee+bnum+,cmp);
int x,y;
while(tail<n-)
{
cur++;
x=find(usee[cur].from),y=find(usee[cur].to);
if(x!=y)
{
tail++;
edge_add(usee[cur].from,usee[cur].to,usee[cur].dis);
edge_add(usee[cur].to,usee[cur].from,usee[cur].dis);
f[x]=y;
}
}
tarjan(,);
int q=qread();
while(q--)
{
from=qread(),to=qread();
dfn[from]<dfn[to]?printf("%d\n",tarjan_lca(from,to)):printf("%d\n",tarjan_lca(to,from));
}
return ;
}

AC日记——货车运输 codevs的更多相关文章

  1. AC日记——约瑟夫问题 codevs 1282

    1282 约瑟夫问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 有编号从1到N的N个小 ...

  2. AC日记——[NOIP2015]运输计划 cogs 2109

    [NOIP2015] 运输计划 思路: 树剖+二分: 代码: #include <cstdio> #include <cstring> #include <iostrea ...

  3. AC日记——丑数 codevs 1246

    1246 丑数 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 对于一给定的素 ...

  4. AC日记——砍树 codevs 1388

    1388 砍树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 伐木工人米尔科需要砍倒M米长的木 ...

  5. AC日记——元素查找 codevs 1230

    1230 元素查找  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给出n个正整数,然后有 ...

  6. AC日记——石子归并 codevs 1048

    1048 石子归并  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 有n堆石子排成一列,每堆石子 ...

  7. AC日记——搞笑世界杯 codevs 1060(dp)

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...

  8. AC日记——产生数 codevs 1009 (弗洛伊德)(组合数学)

    1009 产生数 2002年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descriptio ...

  9. AC日记——平衡树练习 codevs 4244

    4244 平衡树练习 思路: 有节操的人不用set也不用map: 代码: #include <cstdio> #include <cstring> #include <i ...

随机推荐

  1. 01 Web框架介绍

    一.Web框架本质 所有的web应用程序本质上都是socket,用户的浏览器其实就是一个socket客户端. python中常用的web框架有: Django Flask web.py WSGI(we ...

  2. linux 下 docker-compose安装

    docker和dockers-compose的版本兼容对照 以下是我的服务器的相关信息 linux版本 [root@izbp16fm097gaw3tdaog2wz bin]# cat /proc/ve ...

  3. How to Install Zabbix Server on Centos6.7

    Prerequisite Environment First you must use your Subscription Manager to enable SCL: [root@fileserve ...

  4. 服务器TIME_WAIT和CLOSE_WAIT分析和解决办法

    先上两张图: 查看TIME_WAIT和CLOSE_WAIT数的命令: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a ...

  5. nrf51822微信开发2:[转]airkiss/airsync介绍

    "微信蓝牙"专题共分为8部分 1.airkiss/airsync介绍 2.eclipes的j2ee软件使用教程 3.微信公众号使用Dome(airkiss/airsync) 4.新 ...

  6. python列表中的深浅copy

    列表中的赋值和平常的赋值是不一样的,看下面的代码: In [1]: a = 1 In [2]: b = a In [3]: a Out[3]: 1 In [4]: b Out[4]: 1 In [5] ...

  7. stm32L0工程建立(HAL+IAR,无cubemx)

    https://files.cnblogs.com/files/CodeWorkerLiMing/STM32HAL%E5%BA%93%E5%AD%A6%E4%B9%A0%E2%80%94%E5%B7% ...

  8. HDU 5614 Baby Ming and Matrix tree 树链剖分

    题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...

  9. Python IO多路复用select模块

    多路复用的分析实例:服务端.客户端 #服务端配置 from socket import * import time import select server = socket(AF_INET, SOC ...

  10. [git 学习篇]git管理的是修改,并非文件

    你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改. 为什么说Git管理的 ...