总的路径长度(微软笔试题)

题目链接:http://hihocoder.com/contest/mstest2015sept2/problem/2

题目:

题目2 : Total Highway Distance

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.

The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City 3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.

During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?

输入

Line 1: two integers N and M.

Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.

Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.

EDIT i j k, indicating change the length of the highway between city i and city j to k miles.

QUERY, for querying the THD.

For 30% of the data: 2<=N<=100, 1<=M<=20

For 60% of the data: 2<=N<=2000, 1<=M<=20

For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.

输出

For each QUERY operation output one line containing the corresponding THD.

样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出
10
14
12

分析:

你可以想象一个图,一条路把城市分成路左边和路右边的,这个i就是路的两个端点中的孩子节点(两个端点一个位于孩子端点一个位于子节点),思想是这样,具体里面的+1-1什么的可能不太准确
因为是n个城市n-1道路,其实每个城市去其他城市的道路都是确定的,(没有必要用dijkstra等算法求出最短路径,因为会有唯一路径)所以先第一遍就是求出所有城市遍历的时候道路被走了多少遍,到道路Edit的时候增加或减少改变的值乘以遍数即可。
把路径看成一棵树哈:每条边把树分成两部分,一边x个节点,一边y个节点,边权为c,则这条边的贡献就是x*y*c;dfs一遍就行啦

举个栗子:

题目中的1--2--3,其中以1为根,计算它的子节点有2个,剩余的结点是3 - 2 = 1,说明1和它直接的子节点2的道路1--2这条路一边的城市有2个,一边的城市有1个,它们在THD中占2 * 1 * 2(cost) = 4比重。

再计算1的子树2为根的情况,它的子节点有1个,剩余的结点是3 - 1 = 2个,说明2和它直接的子节点3的道路2--3这一条路一边的城市有1个,一边的城市有2个,它们在THD中占1 * 2 * 3(cost) = 6比重。

所以结果是4 + 6 为10。

当EDIT 1 2 4后,原来的1--2这条路的2 * 1 * 2要变为 2 * 1 * 4,所以大小变化是2 * 1 * (4 - 2)为4,所以10 + 4 = 14。

当EDIT2 3 2 后,原来2--3这条路的1 * 2 * 3变成了1 * 2 * 2,大小变化是1 * 2 * (2 - 3)为-1,所以14 - 2 = 12。

扩展一下:

对于如下的图


对于城市1来说,它的子树包括2这棵子树和其他子树,其中2这棵子书(包括2)有(2 + c2 + c3)个城市,那么对于1--2这条路来说,道路两旁的城市数为(2 + c2 + c3)和(n - 2 - c2 - c3)个(或者是1 + c1个也是一样的),对于THD的定义来说,它们占(2 + c2 + c3) * (n - 2 - c2 - c3) * c大小。


代码(未提交测试过):

#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<math.h>
#include<climits>
#include<map>
#include<set>
using namespace std;
int DFS(int root, int n,int** cost, int** count){
	int sum = 0;
	for (int i = 1; i <= n; ++i){
		if (cost[root][i] != 0){
			cost[i][root] = 0;//路只能找一遍,有方向的,找了这个方向就关闭另一个方向
			count[root][i] ++;
			count[root][i] += DFS(i, n, cost, count);
		}
		sum += count[root][i];
	}
	return sum;
}

int main(){
	//freopen("F://Temp/input.txt", "r", stdin);
	int n, m;
	cin >> n >> m;
	int **cost = new int*[n + 1];//注意这里二维数组的new的方法
	for (int i = 0; i <= n; ++i){
		cost[i] = new int[n + 1];
	}
	int **count = new int*[n + 1];
	for (int i = 0; i <= n; ++i){
		count[i] = new int[n + 1];
	}
	for (int i = 0; i <= n; ++i){
		for (int j = 0; j <= n; ++j){
			cost[i][j] = count[i][j] = 0;
		}
	}
	for (int i = 0; i < n - 1; ++i){
		int s, t, c;
		cin >> s >> t >> c;
		cost[s][t] = cost[t][s] = c;//为了让1能遍历所有的路
	}
	DFS(1,n ,cost, count);//point1
	int sum = 0;
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= n; ++j){
			sum += cost[i][j] * count[i][j] * (n - count[i][j]);//point2
		}
	}
	for (int i = 0; i < m; ++i){
		string kind;
		cin >> kind;
		if (kind == "QUERY")
			cout << sum << endl;
		else if (kind == "EDIT"){
			int s, t, c;
			cin >> s >> t >> c;
			sum += (c - cost[s][t]) * count[s][t] * (n - count[s][t]);//point3
		}
	}
	return 0;
}
其中,只对形成的树DFS进行了一遍遍历,复杂度为O(n * n),把所有的路会走的遍数存在了count[][]数组后,而后m次的查询都是O(1)时间能够解决的。

——Apie陈小旭

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值