UVA 11354 Bond(prim+LCA,4级)
来源:互联网 发布:数据分析师cda看什么书 编辑:程序博客网 时间:2024/05/17 22:58
Description
B
NEXT Generation Contest 4
Time Limit – 8 secs
Bond
Once again, James Bond is on his way to saving the world. Bond's latest mission requires him to travel between several pairs of cities in a certain country.
The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads. Bond is going to steal a vehicle, and drive along the roads from city s to city t. The country's police will be patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the police.
More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is defined as the maximum dangerousness of any road on this path.
Now, it's your job to help Bond succeed in saving the world by finding the least dangerous paths for his mission.
Input
There will be at most 5 cases in the input file.
The first line of each case contains two integers N, M (2 ≤ N≤ 50000, 1≤ M ≤ 100000) – number of cities and roads. The next M lines describe the roads. The i-th of these lines contains three integers: xi, yi, di (1 ≤ xi, yi ≤ N, 0 ≤ di ≤ 109) - the numbers of the cities connected by the ith road and its dangerousness.
Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by Q lines, the i-th of which contains two integers si andti (1 ≤ si, ti ≤ N, si != ti).
Consecutive input sets are separated by a blank line.
Output
For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between cities si and ti. Consecutive output blocks are separated by a blank line.
The input file will be such that there will always be at least one valid path.
Sample Input
Output for Sample Input
4 5
1 2 10
1 3 20
1 4 100
2 4 30
3 4 10
2
1 4
4 1
2 1
1 2 100
1
1 2
20
20
100
ProblemSetter: Ivan Krasilnikov
思路:最小瓶颈路。堆+prim+LCA
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#define ll(x) (1<<x)#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof f)using namespace std;const int mp=5e4+9;const int me=5e5+9;const int oo=0x3f3f3f3f;class Edge{public: int v,next,u,w; Edge(){} Edge(int _u,int _v,int _w){u=_u;v=_v;w=_w;} bool operator<(const Edge&t)const { return w>t.w; }};class Graph_tree{public: int head[mp],edge; Edge e[me*2]; int n,MST; void clear() { clr(head,-1);edge=0; } void add(int u,int v,int w) { e[edge].w=w;e[edge].v=v;e[edge].u=u;e[edge].next=head[u];head[u]=edge++; } bool vis[mp];int dis[mp],pre[mp],dep[mp]; void prim() { priority_queue<Edge>Q; FOR(i,0,n)vis[i]=0,dis[i]=oo,pre[i]=-1; vis[1]=1;dep[1]=0; int u,v; for(int i=head[1];~i;i=e[i].next) { v=e[i].v; dis[v]=e[i].w; Q.push(Edge(1,v,e[i].w)); }MST=0; while(!Q.empty()) { Edge ret=Q.top();Q.pop(); u=ret.v; if(vis[u])continue; pre[u]=ret.u; dep[u]=dep[ret.u]+1; vis[u]=1; dis[u]=ret.w;MST+=ret.w; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(!vis[v]&&dis[v]>e[i].w) { dis[v]=e[i].w; Q.push(Edge(u,v,e[i].w)); } } } } int maxcost[mp][32],anc[mp][32]; void init_RMQ() { FOR(i,1,n) { anc[i][0]=pre[i];maxcost[i][0]=dis[i]; for(int j=1;ll(j)<=n;++j)anc[i][j]=-1; } for(int j=1;ll(j)<=n;++j) FOR(i,1,n) if(anc[i][j-1]!=-1) { int fa=anc[i][j-1]; anc[i][j]=anc[fa][j-1]; maxcost[i][j]=max(maxcost[i][j-1],maxcost[fa][j-1]); } } int RMQ(int u,int v) { if(dep[u]<dep[v])swap(u,v); int log; for(log=1;ll(log)<=dep[u];++log);--log; int ans=-1; for(int i=log;i>=0;--i) if(dep[u]-ll(i)>=dep[v]) { ans=max(ans,maxcost[u][i]);u=anc[u][i]; } if(u==v)return ans; for(int i=log;i>=0;--i) if(anc[u][i]!=-1&&anc[u][i]!=anc[v][i]) { ans=max(maxcost[u][i],ans);u=anc[u][i]; ans=max(maxcost[v][i],ans);v=anc[v][i]; } ans=max(ans,dis[u]); ans=max(ans,dis[v]); return ans; } int LCA(int u,int v) { int dis1=-1,dis2=-1; if(dep[u]<dep[v])swap(u,v); while(dep[u]!=dep[v]) { dis1=max(dis1,dis[u]); u=pre[u]; } while(u^v) { dis1=max(dis1,dis[u]); dis2=max(dis[v],dis2); u=pre[u];v=pre[v]; } return max(dis1,dis2); }}sp;int main(){ int n,m,a,b,c,cas=0; while(~scanf("%d%d",&n,&m)) { sp.clear();sp.n=n; FOR(i,1,m) { scanf("%d%d%d",&a,&b,&c); sp.add(a,b,c);sp.add(b,a,c); } sp.prim(); int Q; if(cas)printf("\n");++cas; scanf("%d",&Q); sp.init_RMQ(); while(Q--) { scanf("%d%d",&a,&b); printf("%d\n",sp.RMQ(a,b)); } }}
- UVA 11354 Bond(prim+LCA,4级)
- UVA 11354 Bond (LCA + MST) (堆优化的Prim)
- UVA 11354 Bond (MST + LCA)
- UVA 11354 Bond(MST + LCA)
- UVA - 11354 Bond(生成树+LCA)
- UVA 11354Bond(MST+LCA)
- UVa 11354 Bond (MST 树链剖分 LCA/RMQ)
- UVA 11354 - Bond 最小树/LCA/瓶颈路
- UVA 11354 Bond(MST+LCA/二进制优化)
- 【UVa】11354 Bond 最小生成树,动态LCA,倍增思想
- UVA 11354 Bond 瓶颈路 最小生成树+LCA类似
- UVA 11354 Bond(最小生成树+LCA倍增)
- UVA 11354 Bond(最小生成树+LCA)
- Uva 11354 Bond(最小生成树+LCA)
- UVa 11354 Bond 最小生成树+LCA倍增
- UVA 11354 Bond
- UVA 11354 Bond
- uva 11354 - Bond(树链剖分)
- Android中View绘制流程以及invalidate()等相关方法分析
- [access][jdbc]java连接access数据库
- 成都传智播客java培训10月份新班开班
- 4.给定能随机生成整数1到5的函数,写出能随机生成整数1到7的函数
- 验证模型
- UVA 11354 Bond(prim+LCA,4级)
- 16进制字符串与图片互转
- linux编程--获取文件状态与文件映射mmap .
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- 什么是java序列化,如何实现java序列化?
- Service详解之路01_怒说Service生命周期
- [设计模式笔记]三. 行为型模式--18. Iterator模式(迭代器)对象行为型模式(一)
- MVC 初学者(1)
- 数学之路(3)-机器学习(3)-常用算法-线性规划之几何解法