题目描述

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;
}

[USACO10MAR]伟大的奶牛聚集 BZOJ 1827 树形dp+dfs的更多相关文章

  1. P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  2. [USACO10MAR]伟大的奶牛聚集

    [USACO10MAR]伟大的奶牛聚集 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会. 每个奶牛居住在 N(1<=N& ...

  3. BZOJ_1827_[Usaco2010 Mar]gather 奶牛大集会_树形DP

    BZOJ_1827_[Usaco2010 Mar]gather 奶牛大集会_树形DP 题意:Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来 ...

  4. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  5. [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925

    题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...

  6. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  7. [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...

  8. BZOJ 1040 树形DP+环套树

    就是有n个点n条边,那么有且只有一个环那么用Dfs把在环上的两个点找到.然后拆开,从这条个点分别作树形Dp即可. #include <cstdio> #include <cstrin ...

  9. BZOJ - 2500 树形DP乱搞

    题意:给出一棵树,两个给给的人在第\(i\)天会从节点\(i\)沿着最长路径走,求最长的连续天数\([L,R]\)使得\([L,R]\)为起点的最长路径极差不超过m 求\(1\)到\(n\)的最长路经 ...

随机推荐

  1. HashMap与ConcurrentHashMap的区别(转)

    从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. 在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从 ...

  2. 详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化

    在数据库有外键的时候,使用 select_related() 和 prefetch_related() 可以很好的减少数据库请求的次数,从而提高性能.本文通过一个简单的例子详解这两个函数的作用. 1. ...

  3. redis实现发布订阅

    订阅者 #!/usr/bin/env python # -*- coding:utf-8 -*- import redis r = redis.Redis(host='192.168.11.119') ...

  4. 手动去除uTorrent中广告的步骤(V3.4.9依然有效)

    1.开打utorrent,依次点击选项->设置->高级. 在“高级”界面中,你会看到“过滤器”,在“过滤器”右侧的框中输入“offers”. 这时会在下面框中看到“offers.left_ ...

  5. 3D Text & GUI Text & GUI Texture

    [Unity3D Text] 3D Text通过"Text Mesh"组件."MeshRender共同完成",通过如下方法要以创建一个3D Text. 3D T ...

  6. 【HDU4303】Hourai Jeweled

    题意 有一棵n个结点的树,每个结点都有一个值,没一条边都有一个颜色.如果某条路径上,相邻的边颜色不同,那么把这路径上所有的点的值加起来. 输出所有符合条件的路径上值的和. n<=300000. ...

  7. codeforce452DIV2——E. Segments Removal

    题目 Vasya has an array of integers of length n. Vasya performs the following operations on the array: ...

  8. 【bzoj1565】[NOI2009]植物大战僵尸

    1565: [NOI2009]植物大战僵尸 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2164  Solved: 1001[Submit][Stat ...

  9. Linux 搭建NFS文件服务器实现文件共享

    我们接着玩Linux,O(∩_∩)O哈哈~ 1.什么是nfs NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP ...

  10. springBoot集成 quartz动态定时任务

    项目中需要用到定时任务,考虑了下java方面定时任务无非就三种: 用Java自带的timer类.稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和 ...