hihoCoder #1238 Total Highway Distance

本文深入探讨了游戏开发中利用城市连接优化的策略,通过构建模拟建设环境,玩家需要构建N个城市并通过N-1条公路相连。文章详细介绍了计算总公路距离(THD)的方法,并提供了在游戏过程中调整公路长度的技术,最终目标是在给定的限制条件下计算最新的总公路距离。

Description

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?

Input

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.

Output

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

Sample Input

3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY

Sample Output

10
14
12

Solution:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <vector>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 #define MAXN 100010
 8 
 9 
10 vector<int> edges[MAXN];
11 vector<long long> d[MAXN];
12 int child[MAXN];
13 int fa[MAXN];
14 long long thd = 0;
15 
16 int N, M;
17 
18 
19 void addEdge(int u, int v, int w) {
20     edges[u].push_back(v);
21     d[u].push_back(w);
22 }
23 
24 void addDEdge(int u, int v, int w) {
25     addEdge(u, v, w);
26     addEdge(v, u, w);
27 }
28 
29 void dfs(int u, vector<int> &vis) {
30     vis[u] = 1;
31     child[u] = 0;
32     for (int i = 0; i < edges[u].size(); ++i) {
33         int v = edges[u][i];
34         if (!vis[v]) {
35             fa[v] = u;
36             dfs(v, vis);
37             child[u] += child[v] + 1;
38             thd += d[u][i] * (child[v] + 1)*(N - 1 - child[v]);
39         }
40     }
41 }
42 
43 int update(int u, int v, int k) {
44     int old = 0;
45     for (int i = 0; i < edges[u].size(); ++i) {
46         if (edges[u][i] == v) {
47             old = d[u][i];
48             d[u][i] = k;    
49         }
50     }
51 
52     for (int i = 0; i < edges[v].size(); ++i) {
53         if (edges[v][i] == u) {
54             d[v][i] = k;
55         }
56     }
57 
58     return old;
59 }
60 
61 int main() {
62     
63     scanf("%d%d", &N, &M);
64     for (int i = 0; i < N - 1; ++i) {
65         int u, v, w;
66         scanf("%d%d%d", &u, &v, &w);
67         addDEdge(u, v, w);
68     }
69     vector<int> vis(N+1, 0);
70     fa[1] = 0;
71     dfs(1, vis);
72 
73     for (int k = 1; k <= M; ++k) {
74         char op[10];
75         scanf("%s", op);
76         if (strcmp(op, "EDIT") == 0) {
77             int u, v, k;
78             scanf("%d%d%d", &u, &v, &k);
79             int old = update(u, v, k);
80             if (fa[v] == u) {
81                 thd += (long long)(k - old) * (child[v] + 1) * (N - 1 - child[v]);
82             }
83             else {
84                 thd += (long long)(k - old) * (child[u] + 1) * (N - 1 - child[u]);
85             }
86         }
87         else {
88             printf("%lld\n", thd);
89         }
90     }
91 }

 

转载于:https://www.cnblogs.com/liew/p/4871271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值