最小生成树和倍增法求lca(Uva11354Bond)

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 i-th 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 and ti (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
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
Sample Output
20
20
100

简单的翻译一下,给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路。

由于数据范围巨大,我们不能用普通的方法。(不能用二分)

我们可以把它转换成求最小瓶颈生成树(最大边最小)于是就用克鲁斯卡尔来做。

于是我们先求出最小生成树,然后改写成有根树(dfs),用fa[i]和cost[i]表示节点i的父亲的编号,和边权,我们运用倍增的思想,用p[i][j]表示节点i的第2^j祖先的编号,若为-1表示祖先不存在,maxcost[i][j]表示i节点到他第2^j号祖先的最大权值。

我们用lca的办法,将要查询的节点向上提,更新最大边权就可以了。

代码虽然很长,但是里面有两个模板,代码难度还是比较简单的。

#include<bits/stdc++.h>
#define maxn 600000
#define inf (1124984)
using namespace std;
int head[maxn],book[maxn],deep[maxn],pre[maxn],fa[maxn],mxcost[maxn][30],cost[maxn],p[maxn][30];
int n,m,k,cnt=0;
struct Edge{int from,to,w;}e[maxn];
struct Edg{int to,next,w;}edge[maxn];
int find(int x){return x==pre[x]?x:pre[x]=find(pre[x]);}//并查集 
bool cmp(Edge a,Edge b){return a.w<b.w;}
void add(int u,int v,int w)
{
	edge[++cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt;
	edge[cnt].w=w;
}

void MinTree()//最小生成树,克鲁斯卡尔名字不会打了,就。。 
{
	for(int i=1;i<=n;i++) pre[i]=i;
	sort(e+1,e+m+1,cmp);
	int num=0;
	for(int i=1;i<=m;i++)
	{
		int fx=find(e[i].from),fy=find(e[i].to);
		if(fx!=fy)
		{
			pre[fx]=fy;
			num++;
			add(e[i].from,e[i].to,e[i].w);
			add(e[i].to,e[i].from,e[i].w);
			if(num==n-1) break;
		}
	}
}
void DFS(int x)
{
	book[x]=1;
	for(int i=head[x];i;i=edge[i].next)
	{
		int j=edge[i].to;
		if(!book[j])
		{
			fa[j]=x;
			deep[j]=deep[x]+1;
			cost[j]=edge[i].w;
			DFS(j);
		}
	}
}

void preprocess()
{
	for(int i=1;i<=n;i++)
	{
		p[i][0]=fa[i];mxcost[i][0]=cost[i];
		for(int j=1;(1<<j)<=n;j++) p[i][j]=-1;//没有父亲的节点初始化为-1 
	}
	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i<=n;i++)
		{
			int fat=p[i][j-1];
			if(fat!=-1)
			{
				p[i][j]=p[fat][j-1];
				mxcost[i][j]=max(mxcost[i][j-1],mxcost[fat][j-1]);//最大的边权等于这个点到他的2^j-1和2^j-1到2^j的最大边权 
			}
		}
/*	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i<=n;i++)
			cout<<mxcost[i][j]<<"~"<<i<<j<<"\n";*/
}

int query(int a,int b)//套用lca的模板 
{
	if(deep[a]<deep[b]) swap(a,b);
	int i=0,ans=0;
	while((1<<i)<=n) i++;
	i--;//减小边界为2^i-1 
	int depth=deep[a]-deep[b];
	for(int j=i;j>=0;j--)
		if((1<<j)&depth) 
		{ans=max(ans,mxcost[a][j]);a=p[a][j];}//注意更新的顺序 
	if(b==a) return ans;
	for(int j=i;j>=0;j--)
	{
		if(p[a][j]!=p[b][j])
		{
			ans=max(ans,mxcost[a][j]);
			ans=max(ans,mxcost[b][j]);
			a=p[a][j],b=p[b][j];	
		}
	}
	ans=max(ans,mxcost[a][0]);
	ans=max(ans,mxcost[b][0]);
	return ans;
}
//一定要初始化 
void init()
{
	memset(e,0,sizeof(e));
	memset(edge,0,sizeof(edge));
	memset(book,0,sizeof(book));
	memset(deep,0,sizeof(deep));
	memset(fa,0,sizeof(fa));
	memset(head,0,sizeof(head));
	memset(mxcost,0,sizeof(mxcost));
	memset(cost,0,sizeof(cost));
	cnt=0;
}

int main()
{
	int kase=0;
	while(cin>>n>>m)
	{
		init();
		for(int i=1;i<=m;i++)
			cin>>e[i].from>>e[i].to>>e[i].w;
		MinTree();
		deep[1]=1;fa[1]=-1;
		DFS(1);
		/*for(int i=1;i<=4;i++)
			cout<<deep[i]<<"~"<<i<<"\n";*/
		preprocess(); 
		int k;cin>>k;
		if(++kase!=1) cout<<"\n";
		while(k--)
		{
			int t1,t2;
			cin>>t1>>t2;
			cout<<query(t1,t2)<<"\n";
		}
	}
	return 0;
}

 

 137行,看了刘老师的标程121行,我太菜了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值