题目描述

Bessie is planning the annual Great Cow Gathering for cows all across the country and, of course, she would like to choose the most convenient location for the gathering to take place.

Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会。当然,她会选择最方便的地点来举办这次集会。

Each cow lives in one of N (1 <= N <= 100,000) different barns (conveniently numbered 1..N) which are connected by N-1 roads in such a way that it is possible to get from any barn to any other barn via the roads. Road i connects barns A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N) and has length L_i (1 <= L_i <= 1,000). The Great Cow Gathering can be held at any one of these N barns. Moreover, barn i has C_i (0 <= C_i <= 1,000) cows living in it.

每个奶牛居住在 N(1<=N<=100,000) 个农场中的一个,这些农场由N-1条道路连接,并且从任意一个农场都能够到达另外一个农场。道路i连接农场A_i和B_i(1 <= A_i <=N; 1 <= B_i <= N),长度为L_i(1 <= L_i <= 1,000)。集会可以在N个农场中的任意一个举行。另外,每个牛棚中居住者C_i(0 <= C_i <= 1,000)只奶牛。

When choosing the barn in which to hold the Cow Gathering, Bessie wishes to maximize the convenience (which is to say minimize the inconvenience) of the chosen location. The inconvenience of choosing barn X for the gathering is the sum of the distances all of the cows need to travel to reach barn X (i.e., if the distance from barn i to barn X is 20, then the travel distance is C_i*20). Help Bessie choose the most convenient location for the Great Cow Gathering.

在选择集会的地点的时候,Bessie希望最大化方便的程度(也就是最小化不方便程度)。比如选择第X个农场作为集会地点,它的不方便程度是其它牛棚中每只奶牛去参加集会所走的路程之和,(比如,农场i到达农场X的距离是20,那么总路程就是C_i*20)。帮助Bessie找出最方便的地点来举行大集会。

Consider a country with five barns with [various capacities] connected by various roads of varying lengths. In this set of barns, neither barn 3 nor barn 4 houses any cows.

1 3 4 5

@--1--@--3--@--3--@[2]

[1] |

2 | @[1] 2 Bessie can hold the Gathering in any of five barns; here is the table of inconveniences calculated for each possible location:

Gather ----- Inconvenience ------

Location B1 B2 B3 B4 B5 Total

1 0 3 0 0 14 17

2 3 0 0 0 16 19

3 1 2 0 0 12 15

4 4 5 0 0 6 15

5 7 8 0 0 0 15

If Bessie holds the gathering in barn 1, then the inconveniences from each barn are:

Barn 1 0 -- no travel time there!

Barn 2 3 -- total travel distance is 2+1=3 x 1 cow = 3 Barn 3 0 -- no cows there!

Barn 4 0 -- no cows there!

Barn 5 14 -- total travel distance is 3+3+1=7 x 2 cows = 14 So the total inconvenience is 17.

The best possible convenience is 15, achievable at by holding the Gathering at barns 3, 4, or 5.

输入输出格式

输入格式:

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains a single integer: C_i

* Lines N+2..2*N: Line i+N+1 contains three integers: A_i, B_i, and L_i

第一行:一个整数 N 。

第二到 N+1 行:第 i+1 行有一个整数 C_i

第 N+2 行到 2*N 行:第 i+N+1 行为 3 个整数:A_i,B_i 和 L_i。

输出格式:

* Line 1: The minimum inconvenience possible

第一行:一个值,表示最小的不方便值。

输入输出样例

输入样例#1:
复制

5
1
1
0
0
2
1 3 1
2 3 2
3 4 3
4 5 3
输出样例#1: 复制

15

说明

感谢@用户名已存在1 提供翻译

用 dp[ u ]表示到u节点的花费值;

对于u的子节点v来说,

我们可以递推出dp[ v ];

cnt 是总的数量,siz是子树的大小,

所以我们可以先dfs一边求出siz的大小;

然后再dfs求出dp的值,取minn即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-11
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll n, m;
ll ans = 99999999999999999;
ll cnt;
ll tot;
ll head[maxn];
ll siz[maxn];
ll c[maxn];
ll dp[maxn];
ll dis[maxn];
struct node {
int v, w, nxt;
}e[maxn]; void addedge(int u, int v, int w) {
e[++tot].v = v; e[tot].w = w; e[tot].nxt = head[u];
head[u] = tot;
} void dfs1(int u, int fa) {
siz[u] = c[u];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == fa)continue;
dfs1(v, u);
siz[u] += siz[v];
dis[u] += dis[v] + siz[v] * e[i].w;
}
} void dfs2(int u, int fa) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == fa)continue;
dp[v] = dp[u] - siz[v] * e[i].w + (cnt - siz[v])*e[i].w;
ans = min(ans, dp[v]); dfs2(v, u);
}
} int main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
n = rd();
for (int i = 1; i <= n; i++) {
rdllt(c[i]); cnt += c[i];
}
for (int i = 1; i < n; i++) {
int u, v, w; u = rd(); v = rd(); w = rd();
addedge(u, v, w); addedge(v, u, w);
}
dfs1(1, 0);
dp[1] = dis[1];
ans = min(ans, dp[1]);
dfs2(1, 0);
printf("%lld\n", ans);
return 0;
}

最新文章

  1. sqlalchemy学习
  2. 严重: Exception sending context initialized event to listener instance of class
  3. Oracle内存管理技术
  4. 【代码笔记】iOS-获得徐家汇的天气预报
  5. 学习Git的总结与体会
  6. sax/dom/jdom/dom4j的区别
  7. 一行命令搞定node.js 版本升级
  8. iOS网络开发之AFNetworking
  9. Error in Android Studio - &quot;Default Activity Not Found&quot;
  10. foreach的指针问题
  11. mysqldump中master-data和dump-slave的区别
  12. 用U盘安装系统
  13. cocos2dx3.4 导出节点树到XML文件
  14. CSU1312:榜单(模拟)
  15. first-,second- and third-class value
  16. mysql 创建用户与授权、修改密码
  17. 手动的写一个structs
  18. FoxMail邮件设置
  19. Linux技术图谱
  20. python测试开发django-54.xadmin添加自定义页面

热门文章

  1. ASP.NET MVC 和 WebForm的权限控制
  2. C++ 输出精度和输出小数点位数
  3. Django----Rest Framework框架
  4. Mr_matcher的细节2
  5. Appium混合应用测试
  6. Controlling Session Behavior in Asp.Net MVC4
  7. 特殊的HttpApplication事件处理
  8. Discrete cosine transform(离散余弦转换)
  9. ArcGIS Engine 中对栅格数据的波段信息统计 (转)
  10. leetcode Submission Details