您的位置:首页 > 其它

1827: [Usaco2010 Mar]gather 奶牛大集会

2016-06-01 16:49 351 查看

1827: [Usaco2010 Mar]gather 奶牛大集会

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 906  Solved: 409

[Submit][Status][Discuss]

Description

Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会。当然,她会选择最方便的地点来举办这次集会。每个奶牛居住在 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)只奶牛。在选择集会的地点的时候,Bessie希望最大化方便的程度(也就是最小化不方便程度)。比如选择第X个农场作为集会地点,它的不方便程度是其它牛棚中每只奶牛去参加集会所走的路程之和,(比如,农场i到达农场X的距离是20,那么总路程就是C_i*20)。帮助Bessie找出最方便的地点来举行大集会。
考虑一个由五个农场组成的国家,分别由长度各异的道路连接起来。在所有农场中,3号和4号没有奶牛居住。 


Input

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

Output

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

Sample Input

5

1

1

0

0

2

1 3 1

2 3 2

3 4 3

4 5 3

Sample Output

15

HINT

Source

Gold

[Submit][Status][Discuss]


通过dfs树形dp,不多说



#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

const int maxn = 1E5 + 10;
typedef long long LL;
const LL INF = 1E18;

struct E{
int to,w;
};

int n;
LL ans = INF,tot,t[maxn],x[maxn];

vector <E> v[maxn];

void dfs1(int k,int from)
{
for (int i = 0; i < v[k].size(); i++) {
int to = v[k][i].to;
if (to == from) continue;
LL road = v[k][i].w;
dfs1(to,k);
x[k] += x[to];
t[k] += t[to] + x[to]*road;
}
}

void dfs2(int k,int from,LL road)
{
if (k != 1) t[k] = t[from] - road*x[k] + road*(tot - x[k]);
ans = min(ans,t[k]);
for (int i = 0; i < v[k].size(); i++) {
int to = v[k][i].to;
if (to == from) continue;
dfs2(to,k,v[k][i].w);
}
}

int main()
{
#ifdef YZY
freopen("yzy.txt","r",stdin);
#endif

cin >> n;
for (int i = 1; i <= n; i++) scanf("%lld",&x[i]),tot += x[i];
for (int i = 1; i < n; i++) {
int a,b,c; scanf("%d%d%d",&a,&b,&c);
v[a].push_back((E){b,c});
v[b].push_back((E){a,c});
}
dfs1(1,0);
dfs2(1,0,0);
cout << ans;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: