湘潭大学OJ Defense Tower(贪心/优先队列)

Defense Tower

Accepted : 45   Submit : 85
Time Limit : 3000 MS   Memory Limit : 65536 KB 

Defense Tower

In ICPCCamp, there are n  cities and (n1)  (bidirectional) roads between cities. The i -th road is between the a i  -th and b i  -th cities. It is guaranteed that cities are connected.

In the i -th city, there is a defense tower with power p i  . The tower protects all cities with a road directly connected to city i . However, the tower in city i  does not protect city i  itself.

Bobo would like to destroy all defense towers. When he tries to destroy the tower in city i , any not-destroyed tower protecting city i  will deal damage whose value equals to its power to Bobo.

Find out the minimum total damage Bobo will receive if he chooses the order to destroy the towers optimally.

Input

The input contains at most 30  sets. For each set:

The first line contains an integer n  (1n10 5  ).

The second line contains n  integers p 1 ,p 2 ,,p n   (1p i 10 4  ).

The i -th of the last (n1)  lines contains 2  integers a i ,b i   (1a i ,b i n ).

Output

For each set, an integer denotes the minimum total damage.

Sample Input

3
1 2 3
1 2
2 3
3
1 100 1
1 2
2 3

Sample Output

3
2
代码:

#include
#include
#include
#include
#include
using namespace std;
int a[100005];
vectorE[100005];
struct node
{
    int a,v;
    friend bool operator<(node a,node b)
    {
        return a.vQ;
        for(int i=1;i<=n;i++)
        {
            A[i].a=i;
            scanf("%d",&A[i].v);
            Q.push(A[i]);
        }
        int ans=0;
        for(int i=1;i<=n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            E[x].push_back(y);
            E[y].push_back(x);
        }
        int flag[100005]={0};
        while(!Q.empty())
        {
            int v=Q.top().a;
            //printf("%d\n",v);
            for(int i=0;i

翻译的一塌糊涂。。。。找到一个大神的博客      http://blog.csdn.net/angon823/article/details/51591685

找的思路。。。

你可能感兴趣的:(湘潭大学OJ Defense Tower(贪心/优先队列))