uva 12655 Trucks [LCA](树链剖分+MST)
来源:互联网 发布:入门c语言 编辑:程序博客网 时间:2024/05/21 14:57
The Subtle Balloons Company (SBC) is the main balloon provider for programming contests; it has
huge factories and warehouses, as well as an extensive truck fleet to ensure the contestants’ happiness.
There are lots of competition sites in Nlogonia, and all of them hired SBC for supplying balloons for
their contests. Nlogonia is an archipelago connected by several bridges. Every island of Nlogonia may
have several regional sites and may also house several SBC warehouses.
When planning the routes for balloon deliveries, SBC faced a problem: for safety issues, every
bridge in Nlogonia has some maximum weight limit for vehicles which cross it. And because of the
great net weight of the transported merchandise, SBC operations’ chief asked you to write a program to
determine the maximum weight allowed to be transported between warehouses and competition sites.
Input
The input contains several test cases. The first line of a test case contains three integers N, M and S
which indicate, respectively, the number of islands, the number of bridges that connect the islands and
the number of sites. The islands are numbered from 1 to N.
Each of the next M lines describes a bridge. The description of a bridge consists in a line with
three integers A, B and W, indicating respectively the two islands connected by the bridge and the
maximum weight allowed in that bridge, in tons.
All bridges are two-way roads; every pair of islands is connected by at most one bridge; and it is
possible to reach every other island in the archipelago using only bridges (naturally it may be needed
to pass through other islands to do so).
Each of the next S lines describe a competition site and contains two integers L and H indicating,
respectively, the number of the island where this site is and the number of the island where the
wharehouse which will be used to deliver the balloons to the site is.
Output
For each site in a test case, in the order they were given, your program must produce a single line,
containing a single integer, the biggest weight which can be transported by truck from the warehouse
to the site.
Restrictions
• 2 ≤ N ≤ 2 × 104
• 1 ≤ M ≤ 105
• 1 ≤ S ≤ 5 × 104
• 1 ≤ A, B, L, H ≤ N, A ̸= B, L ̸= H
• 0 ≤ W ≤ 105
Sample Input
4 5 4
1 2 9
1 3 0
2 3 8
2 4 7
3 4 4
1 4
2 1
3 1
4 3
4 5 2
1 2 30
2 3 20
3 4 10
4 1 40
2 4 50
1 3
1 2
Sample Output
7
9
8
7
20
40
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=2e5+9;struct edge{ int u,v,w;} e[maxn],ee[maxn];int cmp(edge a,edge b){ return a.w>b.w;}int n,m,s,a,b;int sz[maxn],fa[maxn],tp[maxn],id[maxn],dep[maxn],son[maxn],idx,v[maxn];int val[maxn<<2],pre[maxn],eused=0;vector<int> mp[maxn];int ff(int x){ return x==pre[x]?x:pre[x]=ff(pre[x]);}void MST(){ sort(e+1,e+m+1,cmp); for(int i=1; i<=m; i++) { int u=ff(e[i].u); int v=ff(e[i].v); if(u!=v) { pre[u]=v; ee[++eused]=e[i]; mp[e[i].u].push_back(e[i].v); mp[e[i].v].push_back(e[i].u); // printf("------%d %d\n",e[i].u,e[i].v); } }}void dfs1(int u,int f,int d){ sz[u]=1; fa[u]=f; dep[u]=d; son[u]=0; for(int i=0; i<mp[u].size(); i++) { int v=mp[u][i]; if(v==fa[u]) continue; dfs1(v,u,d+1); sz[u]+=sz[v]; if(son[u]==0||sz[son[u]]<sz[v]) son[u]=v; }}void dfs2(int u,int tpp){ tp[u]=tpp; id[u]=++idx; if(son[u]) dfs2(son[u],tpp); for(int i=0; i<mp[u].size(); i++) { int v=mp[u][i]; if(v==fa[u]||v==son[u]) continue; dfs2(v,v); }}void pushup(int rt){ val[rt]=min(val[rt<<1],val[rt<<1|1]);}void build(int l,int r,int rt){ if(l==r) { val[rt]=v[l]; return; } int m=l+r>>1; build(lson); build(rson); pushup(rt);}int query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) return val[rt]; int m=l+r>>1; int ans=0x3f3f3f3f; if(L<=m) ans=min(ans,query(L,R,lson)); if(m<R) ans=min(ans,query(L,R,rson)); return ans;}int qq(int u,int v){ // printf("---%d %d\n",u,v); int t1=tp[u],t2=tp[v]; int ans=0x3f3f3f3f; while(t1!=t2) { if(dep[t1]<dep[t2]) { swap(u,v); swap(t1,t2); } ans=min(ans,query(id[t1],id[u],1,idx,1)); u=fa[t1]; t1=tp[u]; } if(u!=v) { if(dep[u]>dep[v]) swap(u,v); ans=min(ans,query(id[son[u]],id[v],1,idx,1)); } return ans;}int main(){ while(~scanf("%d%d%d",&n,&m,&s)) { idx=eused=0; for(int i=1; i<=n; i++) { pre[i]=i; mp[i].clear(); } for(int i=1; i<=m; i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); MST(); dfs1(1,0,1); dfs2(1,1); for(int i=1; i<=eused; i++) { if(dep[ee[i].u]<dep[ee[i].v]) swap(ee[i].u,ee[i].v); v[id[ee[i].u]]=ee[i].w; } build(1,idx,1); while(s--) { scanf("%d%d",&a,&b); printf("%d\n",qq(a,b)); } } return 0;}
- uva 12655 Trucks [LCA](树链剖分+MST)
- UVA 12655 Trucks(MST + LCA)
- UVA 12655 Trucks(MST + LCA)
- uva12655 Trucks (MST + LCA)
- UVa 11354 Bond (MST 树链剖分 LCA/RMQ)
- UVA 11354 Bond(MST + LCA)
- UVA 11354Bond(MST+LCA)
- UVA 11354 Bond (MST + LCA)
- UVA 11354 Bond(MST+LCA/二进制优化)
- UVA 11354 Bond (LCA + MST) (堆优化的Prim)
- UVALive 4960Sensor network(MST+LCA)
- UVALive 4960 Sensor network(MST+LCA)
- 【codevs1519】过路费 mst+lca
- UVALive 6837There is No Alternative (MST + LCA)
- UVA 1395 - Slim Span(MST)
- UVa 1395 - Slim Span(MST)
- UVA 11747 Heavy Cycle Edges(MST)
- UVA 11354 Bond(MST+倍增)
- HDU 4825 字典树
- Android studio配置郭神的LitePal问题解决方法
- TensorFlow Variable, Placeholder 以及激励函数学习小结
- 线程和进程
- 一大波智力题正在靠近~
- uva 12655 Trucks [LCA](树链剖分+MST)
- 字符串查找
- dfs+bfs Children of the Candy Corn
- 不会&还要学的知识点(近期目标?)
- HDU6058-Kanade's sum 链表+思维
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂
- Prime Path
- 【hautoj 1286】czl的复仇计划 【 斐波那契通项公式+求大数的前n位】
- 双向队列deque/栈stack