Codevs 1519 过路费(Mst+Lca)
1519 过路费
时间限制: 1 s
空间限制: 256000 KB
题目等级 : 大师 Master
题目描述 Description
在某个遥远的国家里,有 n个城市。编号为 1,2,3,…,n。这个国家的政府修建了m 条双向道路,每条道路连接着两个城市。政府规定从城市 S 到城市T需要收取的过路费为所经过城市之间道路长度的最大值。如:A到B长度为 2,B到C 长度为3,那么开车从 A经过 B到C 需要上交的过路费为 3。
佳佳是个做生意的人,需要经常开车从任意一个城市到另外一个城市,因此他需要频繁地上交过路费,由于忙于做生意,所以他无时间来寻找交过路费最低的行驶路线。然而, 当他交的过路费越多他的心情就变得越糟糕。 作为秘书的你,需要每次根据老板的起止城市,提供给他从开始城市到达目的城市,最少需要上交多少过路费。
输入描述 Input Description
第一行是两个整数 n 和m,分别表示城市的个数以及道路的条数。
接下来 m 行,每行包含三个整数 a,b,w(1≤a,b≤n,0≤w≤10^9),表示a与b之间有一条长度为 w的道路。
接着有一行为一个整数 q,表示佳佳发出的询问个数。
再接下来 q行,每一行包含两个整数 S,T(1≤S,T≤n,S≠T), 表示开始城市S 和目的城市T。
输出描述 Output Description
输出共q行,每行一个整数,分别表示每个询问需要上交的最少过路费用。输入数据保证所有的城市都是连通的。
样例输入 Sample Input
4 5
1 2 10
1 3 20
1 4 100
2 4 30
3 4 10
2
1 4
4 1
样例输出 Sample Output
20
20
数据范围及提示 Data Size & Hint
对于 30%的数据,满足 1≤ n≤1000,1≤m≤10000,1≤q≤100;
对于 50%的数据,满足 1≤ n≤10000,1≤m≤10000,1≤q≤10000;
对于 100%的数据,满足 1≤ n≤10000,1≤m≤100000,1≤q≤10000;
分类标签 Tags
分治 最小生成树 图论
/*
Slf(spfa优化).
50弃疗了.
跑最小生成树后Slf.
有一个链的测试点卡不过去.
(不搞mst的话还是能卡过去的).
论常数的重要性orz.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define MAXN 100001
using namespace std;
int head[MAXN],n,m,k,cut,father[MAXN],tot,dis[MAXN],q[MAXN];
struct edge{int v,next;int x;}e[MAXN*2];
struct data{int x,y,z;}s[MAXN*2];
bool b[MAXN];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x;
}
int ffind(int x)
{
return x!=father[x]?father[x]=ffind(father[x]):x;
}
void add(int u,int v,int z)
{
e[++cut].v=v;
e[cut].x=z;
e[cut].next=head[u];
head[u]=cut;
}
bool cmp(const data &x,const data &y)
{
return x.z<y.z;
}
int spfa(int s,int t)
{
int u,v;int head1=1,tail=1;
for(int i=1;i<=n;i++) dis[i]=1e10,b[i]=false;q[head1]=s;dis[s]=0;
while(head1<=tail)
{
u=q[head1++];b[u]=false;
if(head1>n+1) head1=1;
for(int i=head[u];i;i=e[i].next)
{
v=e[i].v;
if(dis[v]>max(dis[u],e[i].x))
{
dis[v]=max(dis[u],e[i].x);
if(!b[v])
{
b[v]=true;
if(dis[v]>dis[q[head1]])
{
if(--head1<0) head1=n;
q[head1]=v;
}
else {
q[++tail]=v;
if(tail>n+1) tail=1;
}
}
}
}
}
return dis[t];
}
int main()
{
int x,y,z;
n=read(),m=read();
for(int i=1;i<=n;i++) father[i]=i;
for(int i=1;i<=m;i++)
x=read(),y=read(),z=read(),s[i].x=x,s[i].y=y,s[i].z=z;
sort(s+1,s+m+1,cmp);
for(int i=1;i<=m;i++)
{
x=ffind(s[i].x),y=ffind(s[i].y);
if(x!=y){
tot++;
father[x]=y;
add(s[i].x,s[i].y,s[i].z);
add(s[i].y,s[i].x,s[i].z);
}
if(tot==n-1) break;
}
k=read();
while(k--)
{
x=read(),y=read();
printf("%d\n",spfa(x,y));
}
return 0;
}
/*
mst定理:最小生成树里的最长边最短.
然后lca维护两点距离.
还有不要漏下某种情况.
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define MAXN 100001
#define D 21
using namespace std;
int head[MAXN],n,m,k,cut,father[MAXN],tot,st,fa[MAXN][D+5],deep[MAXN],dis[MAXN][D+5];
struct edge{int v,next;int x;}e[MAXN*2];
struct data{int x,y,z;}s[MAXN*2];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x;
}
int ffind(int x)
{
return x!=father[x]?father[x]=ffind(father[x]):x;
}
void add(int u,int v,int z)
{
e[++cut].v=v;
e[cut].x=z;
e[cut].next=head[u];
head[u]=cut;
}
bool cmp(const data &x,const data &y)
{
return x.z<y.z;
}
void dfs(int u,int f,int d)
{
deep[u]=d+1;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(f!=v){
fa[v][0]=u;
dis[v][0]=e[i].x;
dfs(v,u,d+1);
}
}
return ;
}
void get_father()
{
for(int j=1;j<=D;j++)
for(int i=1;i<=n;i++)
fa[i][j]=fa[fa[i][j-1]][j-1],
dis[i][j]=max(dis[fa[i][j-1]][j-1],dis[i][j-1]);
return ;
}
int get_same(int u,int v)
{
for(int i=0;i<=D;i++)
{
if((1<<i)&v){
tot=max(tot,dis[u][i]);
u=fa[u][i];
}
}
return u;
}
int lca(int u,int v)
{
tot=0;
if(deep[v]>deep[u]) swap(u,v);
u=get_same(u,deep[u]-deep[v]);
if(u==v) return tot;
for(int i=D;i>=0;i--)
{
if(fa[u][i]!=fa[v][i])
{
tot=max(tot,max(dis[u][i],dis[v][i]));
u=fa[u][i];v=fa[v][i];
}
}
tot=max(tot,max(dis[u][0],dis[v][0]));
return tot;
}
int main()
{
int x,y,z;
n=read(),m=read();
for(int i=1;i<=n;i++) father[i]=i;
for(int i=1;i<=m;i++)
x=read(),y=read(),z=read(),s[i].x=x,s[i].y=y,s[i].z=z;
sort(s+1,s+m+1,cmp);
for(int i=1;i<=m;i++)
{
x=ffind(s[i].x),y=ffind(s[i].y);
if(x!=y){
tot++;
father[x]=y;
add(s[i].x,s[i].y,s[i].z);
add(s[i].y,s[i].x,s[i].z);
}
if(tot==n-1) break;
}
dfs(1,1,0);
get_father();
k=read();
while(k--)
{
x=read(),y=read();
printf("%d\n",lca(x,y));
}
return 0;
}
Codevs 1519 过路费(Mst+Lca)的更多相关文章
- codevs 1519 过路费 最小生成树+倍增
/*codevs 1519 过路费 最小生成树+倍增*/ #include<iostream> #include<cstdio> #include<cstring> ...
- codevs 1519 过路费
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 在某个遥远的国家里,有 n个城市.编号为 1,2,3,…,n.这个国家的政府 ...
- 习题:codevs 1519 过路费 解题报告
今天拿了这道题目练练手,感觉自己代码能力又增强了不少: 我的思路跟别人可能不一样. 首先我们很容易就能看出,我们需要的边就是最小生成树算法kruskal算法求出来的边,其余的边都可以删掉,于是就有了这 ...
- codevs1519 过路费(最小生成树+LCA)
1519 过路费 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 在某个遥远的国家里,有 n个城市.编号为 1,2 ...
- CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )
CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...
- Codeforces 1108F MST Unification MST + LCA
Codeforces 1108F MST + LCA F. MST Unification Description: You are given an undirected weighted conn ...
- GYM 101889I(mst+lca)
最小生成树上倍增询问裸的. const int maxn = 2e5 + 5; int n, m, q; //图 struct Edge { int u, v; ll cost; bool opera ...
- [NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)
一开始觉得是网络流..仔细一看应该是最短路,再看数据范围..呵呵不会写...这道题是最大生成树+最近公共祖先.第一次写..表示各种乱.. 因为要求运输货物质量最大,所以路径一定是在最大生成树上的.然后 ...
- CODEVS——T1519 过路费
http://codevs.cn/problem/1519/ 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Desc ...
随机推荐
- 基于keepalived搭建mysql双主高可用
目录 概述 环境准备 keepalived搭建 mysql搭建 mysql双主搭建 mysql双主高可用搭建 概述 传统(不借助中间件)的数据库主从搭建,如果主节点挂掉了,从节点只能读取无法写入,只能 ...
- 今天朋友带领去谷歌ok2eat 餐厅吃免费午餐
中午去队友公司免费吃饭.朋友带我们去她的楼下吃,就算是googler真的也不是所有cafe都知道,感谢朋友带领.
- 在django中部署vue项目,不单独抽离dist文件
1,在django项目下(app所在目录),新建vue项目,使用脚手架构建vue项目,vue create (项目名) 2,构建好以后,配置django: (1),配置settings: · 修改te ...
- python之jupyter notebook
jupyter是一种交互式计算和开发环境的笔记,ipython命令行比原生的python命令行更加友好和高效,还可以运行web版的界面,支持多语言,输出图形.音频.视频等功能. 安装 pip inst ...
- & 位运算总结
一.& 与 a & -a : 可以计算出 a 的二进制形式的第一个 1 出现的位置. eg: 6 & -6 = 0110 & 1010 = 0010
- windows环境下,kafka常用命令
创建topics kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partition 3 - ...
- get获取后台数据
let url = $.getCookie('prefixUrl')+'/currencyRatesManage/getCurrency'; let vm=this; $.ajax({ url: ur ...
- Java注解【四、自定义注解】
语法要求.元注解 元注解: Target-适用范围: Retention-类型:源码注解.编译时注解.运行时注解 Inherited-可继承(只能继承类上的注解,接口.类中的方法都不行) Docume ...
- pandas中Series对象下的str所拥有的方法(df["xx"].str)
在使用pandas的时候,经常要对DataFrame的某一列进行操作,一般都会使用df["xx"].str下的方法,但是都有哪些方法呢?我们下面来罗列并演示一下.既然是df[&qu ...
- 6.Nginx的session一致性(共享)问题配置方案2
1.利用memcached配置session一致性的另外一种方案tengine的会话保持功能 1.1:Tengine会话保持:通过cookie来实现的 该模块是一个负载均衡模块,通过cookie实现客 ...