码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3195 Design the city

时间:2014-08-01 23:07:52      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:des   style   os   io   for   ar   div   line   


倍增法在线LCA.....



ZOJ Problem Set - 3195
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
Author: HE, Zhuobin
Source: ZOJ Monthly, May 2009


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn=50050;

int n,q;

struct Edge
{
	int to,next,cost;
}edge[maxn*2];

int Adj[maxn],Size=0;

void init()
{
	memset(Adj,-1,sizeof(Adj)); Size=0;
}

void Add_Edge(int u,int v,int c)
{
	edge[Size].to=v;
	edge[Size].next=Adj[u];
	edge[Size].cost=c;
	Adj[u]=Size++;
}

int dist[maxn],cq[maxn];
int inq[maxn];

bool spfa()
{
	memset(dist,63,sizeof(dist));
	memset(cq,0,sizeof(cq));
	memset(inq,false,sizeof(inq));
	dist[0]=0; queue<int> q;
	inq[0]=true; q.push(0); cq[0]++;
	while(!q.empty())
	{
		int u=q.front(); q.pop();
		inq[u]=false;
		for(int i=Adj[u];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(dist[v]>dist[u]+edge[i].cost)
			{
				dist[v]=dist[u]+edge[i].cost;
				if(!inq[v])
				{
					inq[v]=true;
					cq[v]++;
					if(cq[v]>=n)
						return false;
					q.push(v);
				}
			}
		}
	}
	return true;
}

int fa[maxn][18];
int deg[maxn];

void BFS(int root)
{
	queue<int> q;
	deg[root]=0;
	fa[root][0]=root;
	q.push(root);
	while(!q.empty())
	{
		int u=q.front(); q.pop();
		for(int i=1;i<18;i++)
		{
			fa[u][i]=fa[fa[u][i-1]][i-1];
		}
		for(int i=Adj[u];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(v==fa[u][0]) continue;
			deg[v]=deg[u]+1;
			fa[v][0]=u;
			q.push(v);
		}
	}
}

int LCA(int u,int v)
{
	if(deg[u]>deg[v]) swap(u,v);
	int hu=deg[u],hv=deg[v];
	int tu=u,tv=v;
	for(int det=hv-hu,i=0;det;i++,det/=2)
	{
		if(det&1) tv=fa[tv][i];
	}
	if(tu==tv) return tu;
	for(int i=17;i>=0;i--)
	{
		if(fa[tu][i]==fa[tv][i]) continue;
		tu=fa[tu][i];
		tv=fa[tv][i];
	}
	return fa[tu][0];
}

int main()
{
	bool flag=false;
	while(scanf("%d",&n)!=EOF)
	{
		if(flag) putchar(10);
		else flag=true;
		init();
		int a,b,c;
		for(int i=0;i<n-1;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			Add_Edge(a,b,c);
			Add_Edge(b,a,c);
		}
		spfa();
		BFS(0);
		scanf("%d",&q);
		while(q--)
		{
			scanf("%d%d%d",&a,&b,&c);
			int lca=LCA(a,b);
			int ab=dist[a]+dist[b]-dist[lca]*2;
			lca=LCA(a,c);
			int ac=dist[a]+dist[c]-dist[lca]*2;
			lca=LCA(b,c);
			int bc=dist[c]+dist[b]-dist[lca]*2;
			printf("%d\n",(ab+ac+bc)/2);
		}
	}
	return 0;
}




ZOJ 3195 Design the city,布布扣,bubuko.com

ZOJ 3195 Design the city

标签:des   style   os   io   for   ar   div   line   

原文地址:http://blog.csdn.net/ck_boss/article/details/38342847

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!