HDU 1055(贪心)

#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include
#include
#include
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 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;
}

你可能感兴趣的:(ACM_HDU,贪心)