NOIP 2013 货车运输
来源:互联网 发布:seo技术是什么意思 编辑:程序博客网 时间:2024/04/28 22:34
题目描述 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
由于存在重边,要求的答案为边权的最大值,所以很多边是没有必要的,因此进行一遍最大生成树,删掉那些没用的边,之后每两个询问进行lca查询即可
数据不是很坑,暴力lca即可过
#include<iostream>#include<cstdio>#include<queue>#include<cstdlib>#include<algorithm>#include<cmath>#include<cstring>using namespace std;const int maxn=2333333;struct doubi{ int f,t,d;}edge[maxn];int next[maxn],first[maxn],tot;void build(int f,int t,int d){ edge[++tot].f=f; edge[tot].t=t; edge[tot].d=d; next[tot]=first[f]; first[f]=tot;}int depth[maxn];int parent[maxn];int vlaue[maxn];bool use[maxn];void dfs(int u,int p,int d){ depth[u]=d; parent[u]=p; for(int i=first[u];i;i=next[i]){ int v=edge[i].t; if(v!=p&&!use[v]){ use[v]=1; vlaue[v]=edge[i].d; dfs(v,u,d+1); } }}int ask(int a,int b){ int ans=2147483647; if(depth[a]>depth[b]) swap(a,b); while(depth[b]>depth[a]){ if(parent[b]==-1){ return -1; } ans=min(vlaue[b],ans); b=parent[b]; } while(a!=b){ ans=min(ans,vlaue[a]); ans=min(ans,vlaue[b]); a=parent[a]; b=parent[b]; } if(a==-1){ return -1; } return ans;}struct faq{ int f,t,d;}num[maxn];bool cmp(faq a,faq b){ return a.d>b.d;}int fa[maxn];int find(int a){ if(a!=fa[a]) return fa[a]=find(fa[a]); return a;}int main(){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++){ fa[i]=i; } for(int i=1;i<=m;i++){ scanf("%d%d%d",&num[i].f,&num[i].t,&num[i].d); } sort(num+1,num+1+m,cmp); for(int i=1;i<=m;i++){ int x=find(num[i].f),y=find(num[i].t); if(x!=y){ fa[x]=y; build(num[i].f,num[i].t,num[i].d); build(num[i].t,num[i].f,num[i].d); } } for(int i=1;i<=n;i++){ if(!use[i]){ use[i]=1; dfs(i,-1,0); } } /*for(int i=1;i<=n;i++){ cout<<depth[i]<<endl; }*/ int q; cin>>q; for(int i=1;i<=q;i++){ int a,b; scanf("%d%d",&a,&b); cout<<ask(a,b)<<endl; } return 0;}
0 0
- [NOIP 2013]货车运输
- NOIP 2013 货车运输
- NOIP 2013 货车运输
- NOIP 2013 货车运输
- NOIP 2013 货车运输
- NOIP 2013 【货车运输】
- 【noip 2013】货车运输
- NOIP 2013 货车运输
- NOIP-2013 货车运输
- NOIP 2013 货车运输 树链剖分
- NOIP 2013 货车运输(树上倍增)
- 2013年NOIP提高组 货车运输
- NOIP 2013 货车运输 TYVJ 3115
- NOIP提高组2013-货车运输
- NOIP 2013 Day1 T3 货车运输
- NOIP 2013 货车运输 题解过程
- NOIP 2013 货车运输 题解过程
- [T][5]NOIP 2013 货车运输
- Leetcode -- Candy
- 大规模图的存储(前向星、next数组)
- 2015 Python学习之路
- 关于SQL Server无法连接到服务器问题
- memache集群之虚拟机下的magent
- NOIP 2013 货车运输
- (转)Java单例模式
- ViewPager实现自动滑动和下面小圆点可根据图片大小来自适应
- 蓝懿iOS 技术内容和心得 10。26
- 垃圾回收进程的调试(gc模块)
- C语言-结构体的定义及使用
- 第11章 线程
- ios 中的开源框架
- 笑笑语法(字面量)