ZOJ - 3195 Design the city(倍增法求解,tarjan離線求解)

Design the city

Time Limit: 1 Second      Memory Limit: 32768 KB

Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.

Input

The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.

Output

Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.

Sample Input

4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3

Sample Output

3
2

2
2


題意: 給出一顆樹,求三點最短距離

分析: 求樹上最短距離就是求lca的問題啦~ 那麼答案就是 (lca(a,b)+lca(b,c)+lca(a,c))/2

方法的話有在線的倍增法求lca  之前有寫過一些學習心得 傳送門

AC代碼:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=50050;
struct edge
{
	int u,v,w;
	edge(){}
	edge(int uu,int vv,int ww)
	{
		u=uu,v=vv,w=ww;
	}
};
int father[maxn][35],dis[maxn][35],maxdeep,deep[maxn];
vector<edge>G;
vector<int>v[maxn];
void init(int n)
{
	memset(father,0,sizeof(father));
	memset(dis,0,sizeof(dis));
	memset(deep,0,sizeof(deep));
	maxdeep=log(n*1.0)/log(2.0);
	for(int i=0;i<=n;i++)
	v[i].clear();
	G.clear();
	
}
void addtree(int from,int to,int w)
{
	G.push_back(edge(from,to,w));
	G.push_back(edge(to,from,w));
	int m=G.size();
	v[from].push_back(m-2);
	v[to].push_back(m-1);
}
void bulidtree(int root)
{
	for(int i=1;i<=maxdeep;i++)
	{
		father[root][i]=father[father[root][i-1]][i-1];
		dis[root][i]=dis[father[root][i-1]][i-1]+dis[root][i-1];
		if(father[root][i]==0)
		break;
	}
	for(int i=0;i<v[root].size();i++)
	{
		edge &e=G[v[root][i]];
		if(e.v!=father[root][0])
		{
			deep[e.v]=deep[root]+1;
			father[e.v][0]=root;
			dis[e.v][0]=e.w;
			bulidtree(e.v);
		}
	}
}
int lca(int u,int v)
{
	int ans=0;
	if(deep[u]>deep[v])
	swap(u,v);
	for(int i=maxdeep;i>=0;i--)
	{
		if(deep[u]<deep[v]&&deep[u]<=deep[father[v][i]])
		{
			ans+=dis[v][i];
			v=father[v][i];
		}
	}
	if(u==v)
	return ans;
	for(int i=maxdeep;i>=0;i--)
	{
		if(father[u][i]!=father[v][i])
		{
			ans+=dis[u][i]+dis[v][i];
			u=father[u][i];
			v=father[v][i];
		}
	}
	if(u!=v) ans+=dis[v][0]+dis[u][0];
	return ans;
}
int main()
{
	int n;
	int flag=0;
	while(scanf("%d",&n)==1)
	{
		if(flag)
		printf("\n");
		init(n);
		int root;
		for(int i=0;i<n-1;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			a++,b++;
			father[b][0]=a;
			dis[b][0]=c;
			addtree(a,b,c);
			if(!father[a][0])
			root=a;
		}
		deep[root]=1;
		bulidtree(root);
		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			a++,b++,c++;
			printf("%d\n",(lca(a,b)+lca(b,c)+lca(a,c))/2);
			flag=1;
		}
	}
}
離線 tarjan也差不多 這題有點模板~


#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
const int maxn=50050;
struct edge
{
	int v,w;
	edge(){}
	edge(int vv,int ww)
	{
		v=vv,w=ww;
	}
};
struct node
{
	int u,v,lca;
	node(){}
	node(int uu,int vv,int ll)
	{
		u=uu,v=vv,lca=ll;
	}
};
vector<edge>v[maxn];
vector<node>G;
vector<int>g[maxn];
int vis[maxn],dir[maxn];
int father[maxn];
void init(int n)
{
	for(int i=0;i<=n;i++)
	{
		v[i].clear();
		g[i].clear();
		father[i]=i;
	}
	G.clear();
	memset(vis,0,sizeof(vis));
	memset(dir,0,sizeof(dir));
}
int find(int a)
{
	//if(a==father[a])
	//return a;
	//return father[a]=find(father[a]);
	return a==father[a]?a:father[a]=find(father[a]);
}

void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x!=y)
    father[y]=x;
}
void addnode(int x,int y)
{
	G.push_back(node(x,y,-1));
	G.push_back(node(y,x,-1));
	int m=G.size();
	g[x].push_back(m-2);
	g[y].push_back(m-1);
}
void tarjan(int x)
{
	vis[x]=1;
	for(int i=0;i<v[x].size();i++)
	{
		edge &e=v[x][i];
		if(vis[e.v]) continue;
		dir[e.v]=dir[x]+e.w;
		tarjan(e.v);
		unite(x,e.v);
	}
	for(int i=0;i<g[x].size();i++)
	{
		int y=g[x][i];
		if(vis[G[y].v])
		{
			G[y].lca=G[y^1].lca=find(G[y].v);
		}
	}
}
int lca(int x)
{
	return dir[G[x].u]+dir[G[x].v]-2*dir[G[x].lca];
}
int main()
{
	int n;
	while(scanf("%d",&n)==1)
	{
		init(n);
		for(int i=0;i<n-1;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			a++,b++;
			v[a].push_back(edge(b,c));
			v[b].push_back(edge(a,c));
		}
		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			a++,b++,c++;
			addnode(a,b);
			addnode(a,c);
			addnode(b,c);
		}
		tarjan(1);
		for(int i=0;i<m;i++)
		{
			int a=i*6,b=a+2,c=a+4;
			printf("%d\n",(lca(a)+lca(b)+lca(c))/2);
		}
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章