湘潭大学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<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
int a[100005];
vector<int>E[100005];
struct node
{
    int a,v;
    friend bool operator<(node a,node b)
    {
        return a.v<b.v;
    }
}A[100005];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<=n;i++)E[i].clear();
        priority_queue<node>Q;
        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<E[v].size();i++)
            {
                if(flag[E[v][i]]==0)
                    ans=ans+A[E[v][i]].v;
            }
            flag[v]=1;
            Q.pop();
        }
        printf("%d\n",ans);
    }
}

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

找的思路。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值