【NOIP2013】 货车运输
来源:互联网 发布:饥荒mac 中文版官网 编辑:程序博客网 时间:2024/05/17 01:37
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。
倍增+最大生成树
--kru +并查集
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 100005;const int INF = 2147483640;int n,m,tot = 0;int first[MAXN],next[MAXN],dis[MAXN];int bcfa[MAXN],rank[MAXN],fa[MAXN];int val[MAXN],deep[MAXN];bool use[MAXN];struct edge{ int f,t,v;}e[MAXN],l[MAXN << 1];bool cmp(edge a,edge b){ return a.v > b.v;}void init(){ memset(first,0xff,sizeof(first)); tot = 0; return;}void build(int f,int t,int v){ l[++ tot] = (edge){f,t,v}; next[tot] = first[f]; first[f] = tot; return;}void init_bcj(){ for(int i = 1; i <= n; i ++) bcfa[i] = i; memset(rank,0,sizeof(rank)); return;}int find(int x){ return x == bcfa[x] ? x : bcfa[x] = find(bcfa[x]);}bool same(int x,int y){ return find(x) == find(y);}void merge(int x,int y){ x = find(x); y = find(y); if(rank[x] > rank[y]) swap(x,y); bcfa[x] = y; if(rank[x] == rank[y]) rank[y] ++; return;}void kru(){ init_bcj(); sort(e + 1,e + m + 1,cmp); for(int i = 1; i <= m; i ++) { int f = e[i].f; int t = e[i].t; if(!same(f,t)) { build(f,t,e[i].v); build(t,f,e[i].v); merge(f,t); } } return;}void dfs(int x,int f,int va){ fa[x] = f; val[x] = va; use[x] = true; deep[x] = deep[f] + 1; for(int i = first[x]; i != -1; i = next[i]) { int w = l[i].t; if(use[w]) continue; dfs(w,x,l[i].v); } return;}int ask(int x,int y){ if(!same(x,y)) return -1; int ans = INF; if(deep[x] < deep[y]) swap(x,y); while(deep[x] > deep[y]) { ans = min(ans,val[x]); x = fa[x]; } while(x != y) { ans = min(ans,val[x]); ans = min(ans,val[y]); x = fa[x]; y = fa[y]; } return ans;}int q,f,t;int main(){ init(); scanf("%d %d",&n,&m); for(int i = 1; i <= m; i ++) scanf("%d %d %d",&e[i].f,&e[i].t,&e[i].v); kru(); for(int i = 1; i <= n; i ++) if(!use[i]) dfs(i,0,0); scanf("%d",&q); while(q --) { scanf("%d %d",&f,&t); printf("%d\n",ask(f,t)); } return 0;}
- NOIP2013 货车运输
- NOIP2013货车运输 题解
- 【NOIP2013-D1T3】货车运输
- NOIP2013 货车运输
- [NOIP2013]货车运输
- [NOIp2013] 货车运输
- NOIP2013 货车运输
- NOIP2013 D1T3 货车运输
- 【NOIP2013】 货车运输
- NOIP2013 货车运输
- noip2013 货车运输
- codevs3287: [NOIP2013]货车运输
- noip2013货车运输
- NOIP2013货车运输
- noip2013货车运输
- NOIP2013货车运输
- 【NOIP2013】Day1T3 货车运输
- 【NOIP2013】货车运输
- MarkDown常用语法(Lite)
- VRRP协议
- eclipse 调试,选择编译级别
- HSRP和VRRP工作原理详解
- iOS 信号量详解
- 【NOIP2013】 货车运输
- 分析Redis架构设计
- Linux下activeMQ与c++客户端的部署与应用
- POJ 1961Period (KMP)
- 解决unreal中的rotation控制导致的位置偏移
- RIP、OSPF、EIGRP 区别(学习帖)
- 数学推导______Different Sums( hdu 5847 2016多校第九场)
- Android各大热补丁方案分析和比较
- IOS 如何选择delegate、notification、KVO?