HDU-6393 Traffic Network in Numazu(树链剖分)

版权声明:选经典题目,写精品文章. https://blog.csdn.net/nka_kun/article/details/81675119

                                                                    Traffic Network in Numazu

                                                          Time Limit: 4000/2000 MS (Java/Others)


Problem Description
Chika is elected mayor of Numazu. She needs to manage the traffic in this city. To manage the traffic is too hard for her. So she needs your help. 
You are given the map of the city —— an undirected connected weighted graph with N nodes and N edges, and you have to finish Q missions. Each mission consists of 3 integers OP, X and Y. 
When OP=0, you need to modify the weight of the Xth edge to Y. 
When OP=1, you need to calculate the length of the shortest path from node X to node Y.
 

Input
The first line contains a single integer T, the number of test cases. 
Each test case starts with a line containing two integers N and Q, the number of nodes (and edges) and the number of queries. (3≤N≤105)(1≤Q≤105) 
Each of the following N lines contain the description of the edges. The ith line represents the ith edge, which contains 3 space-separated integers ui, vi, and wi. This means that there is an undirected edge between nodes ui and vi, with a weight of wi. (1≤ui,vi≤N)(1≤wi≤105) 
Then Q lines follow, the ith line contains 3 integers OP, X and Y. The meaning has been described above.(0≤OP≤1)(1≤X≤105)(1≤Y≤105) 
It is guaranteed that the graph contains no self loops or multiple edges.
 

Output
For each test case, and for each mission whose OP=1, print one line containing one integer, the length of the shortest path between X and Y.

题意:给出一棵多了一条边的树,两种操作,一个是修改边权,一个是问两点之间最短路.

思路:树链剖分来做的话多了一条边,我们可以把环中那条边拿出去,用并查集搞一搞.剩下一棵树就可以树链剖分结合线段树来做了. 

询问最短路的时候,要么从x从树上直接到y,要么先从x到拿出去的边的一端,然后从另一端到y,一共三种情况取最小就好.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;

struct side
{
	int u,v,w;
	int ne;
} s[maxn];

struct edge
{
	int l,r;
	ll v;
} e[maxn<<2];

int n,q;
int head[maxn],len;
int val[maxn],hold[maxn],pre[maxn];
int deep[maxn],fa[maxn],ve[maxn],son[maxn],top[maxn],p[maxn],fp[maxn],sz;

void add(int u,int v,int w)
{
	s[len].u = u;
	s[len].v = v;
	s[len].w = w;
	s[len].ne = head[u];
	head[u] = len++;
}

int find(int x)
{
	return pre[x] == x?x:pre[x] = find(pre[x]);
}

void dfs1(int u,int p,int d)
{
	deep[u] = d;
	fa[u] = p;
	ve[u] = 1;
	son[u] = -1;
	for(int i = head[u];i!= -1;i = s[i].ne)
	{
		if(s[i].v == p) continue;
		val[s[i].v] = s[i].w; //把边权值赋给相连的点
		hold[i>>1] = s[i].v;//这条边的权值被哪个点掌握着
		dfs1(s[i].v,u,d+1);
		ve[u]+= ve[s[i].v];
		if(son[u] == -1||ve[s[i].v]> ve[son[u]])
			son[u] = s[i].v;
	}
	return ;
}

void dfs2(int u,int sp)
{
	top[u] = sp;
	p[u] = ++sz;
	fp[p[u]] = u;
	if(son[u] == -1) return ;
	dfs2(son[u],sp);
	for(int i = head[u];i!= -1;i = s[i].ne)
	{
		if(s[i].v == son[u]||s[i].v == fa[u]) continue;
		dfs2(s[i].v,s[i].v);
	}
	return ;
}

void build(int i,int l,int r)
{
	e[i].l = l;e[i].r = r;
	if(l == r)
	{
		e[i].v = val[fp[l]];
		return ;
	}
	
	int mid = (l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
	e[i].v = e[i<<1].v+e[i<<1|1].v;
}

void modify(int i,int pos,int v)
{
	if(pos> e[i].r||pos< e[i].l) return ;
	if(e[i].l == e[i].r)
	{
		e[i].v = v;
		return ;
	}
	modify(i<<1,pos,v);
	modify(i<<1|1,pos,v);
	e[i].v = e[i<<1].v+e[i<<1|1].v;
}

ll query(int i,int l,int r)
{
	if(e[i].r< l||e[i].l> r) return 0;
	if(e[i].l>= l&&e[i].r<= r) return e[i].v;
	return query(i<<1,l,r)+query(i<<1|1,l,r);
}

ll demand(int x,int y)
{
	int fx = top[x];
	int fy = top[y];
	ll ans = 0;
	while(fx!= fy)
	{
		if(deep[fx]< deep[fy])
		{
			swap(fx,fy);
			swap(x,y);
		}
		ans+= query(1,p[fx],p[x]);
		x = fa[fx];
		fx = top[x];
	}
	if(x == y) return ans;
	if(deep[x]> deep[y]) swap(x,y);
	ans+= query(1,p[son[x]],p[y]);
	return ans;
}

void init()
{
	sz = len = 0;
	mem(head,-1);
	for(int i = 0;i<= n;i++) pre[i] = i;
}

int main()
{
	int t;
	cin>>t;
	
	while(t--)
	{
		int su,sv,sc,ss;
		scanf("%d %d",&n,&q);
		init();
		for(int i = 1;i<= n;i++)
		{
			int u,v,w;
			scanf("%d %d %d",&u,&v,&w);
			int fx = find(u);
			int fy = find(v);
			
			if(fx == fy)
			{
				len+= 2;
				ss = i;
				su = u;sv = v;sc = w;
				continue;
			}
			else
				pre[fy] = fx;
			add(u,v,w);
			add(v,u,w);
		}
		
		dfs1(1,-1,1);
		dfs2(1,1);
		
		build(1,1,n);
		
		while(q--)
		{
			int o,x,y;
			scanf("%d %d %d",&o,&x,&y);
			
			if(o == 0)
			{
				x--;
				if(x == ss)
				{
					sc = y;
					continue;
				}
				modify(1,p[hold[x]],y);
			}
			else
			{
				ll ans;
				ans = sc+min(demand(x,su)+demand(y,sv),demand(x,sv)+demand(y,su));
				ans = min(ans,demand(x,y));
				printf("%lld\n",ans);
			}
		}	
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/81675119