#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int N, R;
struct node
{
int father;
double w;
int c;
int t;
}Node[1005];
int find()
{
int pos;
double Max = 0;
for (int i = 1; i <= N; i++)
{
if (Max < Node[i].w&&i!=R)
{
Max = Node[i].w;
pos = i;
}
}
return pos;
}
int main()
{
while (cin >> N >> R, N || R)
{
int ans=0;
int pos;
for (int i = 1; i <= N; i++)
{
cin >> Node[i].c;
Node[i].w = Node[i].c;
Node[i].t = 1;
ans += Node[i].c;
}
for (int i = 1; i <= N - 1; i++)
{
int a, b;
cin >> a >> b;
Node[b].father = a;
}
for (int i = 1; i <N; i++)
{
int pos = find();
int Father;
Father = Node[pos].father;
ans += Node[Father].t*Node[pos].c;
for (int j = 1; j <=N; j++)
{
if (Node[j].father == pos)
{
Node[j].father = Node[pos].father;
}
}
Node[Father].t += Node[pos].t;
Node[Father].c += Node[pos].c;
Node[Father].w = (double)Node[Father].c / Node[Father].t;
Node[pos].w = 0;
}
cout << ans << endl;
}
return 0;
}
HDU 1055(贪心)
最新推荐文章于 2018-10-23 08:40:29 发布