Total Highway Distance(贪心)

题目1 : 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

普通暴力肯定超时,我们有一种特殊的技巧。


对于树中任意一条边e,一定满足这样一个情况:

a

假设A集合有x个节点,则B集合有N-x个节点。

而任何两个节点i,j,若i属于A,j属于B,则其连接路径中一定包含有边e。

i,j的组合有x*(N-x)种,因此可以确定一共有x*(N-x)条路径中包含有边e。换句话说,在所有点对的距离和之中,边e一共计算了x*(N-x)次。

所以我们可以先定义一个根节点(随意,这里我定义1为根节点),用node[i]表示i节点的子树的节点个数(包括i本身)。path[i]表示 i节点到它父亲节点的距离。那么对应path[i]这个距离,我们计算了(node[i])*(n-node[i])次(n表示所有的点)。所以只要用dfs就可以算出来距离和。

下面就是维护的问题,对于一条边的修改,我们定义delta=key-path[a](对于一组输入a,b,key。key为修改后这条边的权值,我们判读a是不是b的儿子,如果不是交换a,b。)最后的结果的变化量就为delta*(path[a])*(n-path[a])。随后path[a]=key(修改路径信息)。要注意用long long。

具体的操作看代码

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int path[100005];//每个节点到他父节点的长度
int level[100005];
int node[100005];
int n,m;
long long tot;
vector<int>to[100005];
vector<int>val[100005];
void dfs(int rt)
{
	node[rt]=1;
	
	for(int i=0;i<to[rt].size();i++)
		{
			if(level[to[rt][i]]==0)
		{
			level[to[rt][i]]=level[rt]+1;
			path[to[rt][i]]=val[rt][i];
			dfs(to[rt][i]);
			node[rt]=node[rt]+node[to[rt][i]];
			tot+=(long long)((long long)node[to[rt][i]])*(path[to[rt][i]])*(n-node[to[rt][i]]);
		}
	}
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		tot=0;
		for(int i=1;i<=n;i++)
		{
			to[i].clear();
			val[i].clear();
		}
		memset(level,0,sizeof(level));
		memset(node,0,sizeof(node));
		int u,v,k;
		for(int i=0;i<n-1;i++)
		{
			scanf("%d%d%d",&u,&v,&k);
			to[u].push_back(v);
			val[u].push_back(k);
			to[v].push_back(u);
			val[v].push_back(k);
		}
		level[1]=1;
		dfs(1);
		char ask[6];
		int a,b,c;
		for(int i=0;i<m;i++)
		{
			scanf("%s",ask);
			if(ask[0]=='Q')
				printf("%lld\n",tot);
			else 
			{
				scanf("%d%d%d",&a,&b,&c);
				int temp;
				if(level[a]<level[b])
				{
					temp=a;
					a=b;
					b=temp;
				}
				long long delta=c-path[a];
				tot+=(long long) (delta*node[a]*(n-node[a]));
				path[a]=c;
			}
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
highway_env是一个用于创建和测试自动驾驶智能体的Python库。它旨在提供一个可扩展的环境,用于开发和评估各种自动驾驶算法highway_env提供了一个高速公路环境,其中包括多车道、交通流量、车辆状态和路标等元素。该库提供了一些基本的环境配置选项,例如车道宽度、车辆速度、车辆密度等,以帮助用户创建符合实际情况的测试场景。 在使用highway_env进行自动驾驶智能体开发时,用户可以通过编程和配置文件来设置环境参数,例如目标速度、道路长度、交通流量分布等。智能体可以通过接受环境状态和奖励信号来决定自己的行动,并通过交互与环境进行学习和优化。 高速公路环境中的多车道和交通流量使得智能体需要学习适应不同的交通状况,并在保持安全的同时尽可能快地到达目的地。因此,highway_env非常适合用于测试各种自动驾驶技术的性能和鲁棒性。 此外,highway_env还提供了一系列实用的函数和方法来帮助用户进行环境和智能体参数的配置、数据记录、可视化等。这些功能大大简化了自动驾驶算法的开发和调试过程,提高了开发效率。 总之,highway_env是一个功能强大的Python库,可以提供可扩展的高速公路环境,用于开发和评估自动驾驶智能体。通过使用highway_env,用户可以方便地创建各种测试场景,并进行自动驾驶算法的开发和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值