AC日记——货车运输 codevs
A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。
第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。
接下来 m 行每行 3 个整数 x、y、z,每两个整数之间用一个空格隔开,表示从 x 号城市到 y 号城市有一条限重为 z 的道路。注意:x 不等于 y,两座城市之间可能有多条道路。
接下来一行有一个整数 q,表示有 q 辆货车需要运货。
接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市运输货物到 y 城市,注意:x 不等于 y。
输出共有 q 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1。
4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3
3
-1
3
对于 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的更多相关文章
- AC日记——约瑟夫问题 codevs 1282
1282 约瑟夫问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有编号从1到N的N个小 ...
- AC日记——[NOIP2015]运输计划 cogs 2109
[NOIP2015] 运输计划 思路: 树剖+二分: 代码: #include <cstdio> #include <cstring> #include <iostrea ...
- AC日记——丑数 codevs 1246
1246 丑数 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 对于一给定的素 ...
- AC日记——砍树 codevs 1388
1388 砍树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 伐木工人米尔科需要砍倒M米长的木 ...
- AC日记——元素查找 codevs 1230
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出n个正整数,然后有 ...
- AC日记——石子归并 codevs 1048
1048 石子归并 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 有n堆石子排成一列,每堆石子 ...
- AC日记——搞笑世界杯 codevs 1060(dp)
题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...
- AC日记——产生数 codevs 1009 (弗洛伊德)(组合数学)
1009 产生数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descriptio ...
- AC日记——平衡树练习 codevs 4244
4244 平衡树练习 思路: 有节操的人不用set也不用map: 代码: #include <cstdio> #include <cstring> #include <i ...
随机推荐
- ProC第三弹
一.前言 我们上面已经了解Windows和Linux下的ProC开发环境,这里我们更进一步去简要介绍下ProC的预编译参数. 二.什么是预编译 预编译过程中,Pro*C/C++会自动生成C或者C++的 ...
- Nordic Collegiate Programming Contest 2015 G. Goblin Garden Guards
In an unprecedented turn of events, goblins recently launched an invasion against the Nedewsian city ...
- 算法导论 第七章 快速排序(python)
用的最多的排序 平均性能:O(nlogn){随机化nlogn} 原地址排序 稳定性:不稳定 思想:分治 (切分左右) 学习方式:自己在纸上走一遍 def PARTITION(A,p,r): x = ...
- BZOJ 2243 染色 树链剖分
题意: 给出一棵树,每个顶点上有个颜色\(c_i\). 有两种操作: C a b c 将\(a \to b\)的路径所有顶点上的颜色变为c Q a b 查询\(a \to b\)的路径上的颜色段数,连 ...
- UOJ 152 汉诺塔 分治
题目链接 题意: 有三根编号为\((1, \, 2, \, 3)\)的柱子,然后第一根柱子上有编号为\(1 \sim n(n \leq 10000)\)的盘子,从上到下第\(i\)个盘子的编号是\(A ...
- CodeForces 570D DFS序 树状数组 Tree Requests
参考九野巨巨的博客. 查询一个子树内的信息,可以通过DFS序转成线形的,从而用数据结构来维护. #include <iostream> #include <cstdio> #i ...
- cinema 4d 包括宝典 --- 改线 循环边 建模布线原则
cinema 4d 一.视图控制与物体控制 1.摇移 alt+鼠标左键 转圈看物体 改变角度 2.平移 alt +鼠标中键 不改变角度 移动 3.推拉 alt+鼠标右键 ...
- Numpy ndarray 的高级索引存在 "bug" ?
Numpy ndarray 高级索引 "bug" ? 话说一天,搞事情,代码如下 import numpy as np tmp = [1, 2, 3, 4] * 2 a, b = ...
- 如何理解logistic函数?
作者:煎挠橙链接:https://www.zhihu.com/question/36714044/answer/78680948来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 51nod1779 逆序对统计
1779 逆序对统计 基准时间限制:1 秒 空间限制:131072 KB lyk最近计划按顺序做n道题目,每道题目都分为很多分数档次,lyk觉得这些题太简单了,于是它想到了一个好玩的游戏. lyk决 ...