2016长城信息杯中国大学生程序设计竞赛中南邀请赛 xtu 1252 Defense Tower

11 篇文章 0 订阅

https://i-blog.csdnimg.cn/blog_migrate/ad6ec671aaebeafbf5d6d3e4366a52a0.png

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

In the i -th city, there is a defense tower with power pi . 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 ( 1n105 ).

The second line contains n integers p1,p2,,pn ( 1pi104 ).

The i -th of the last (n1) lines contains 2 integers ai,bi ( 1ai,bin ).

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;
 
struct p
{
    int num,val;
    friend bool operator<(p a,p b)
    {
        return a.val<b.val;
    }
}df[100005];
vector<int>edge[100005];
 
int main()
{
    int n,x,y,ans;
    int vis[100005];
    while(~scanf("%d",&n))
    {
        for(int i=0;i<=n;i++)
            edge[i].clear();
        priority_queue<p>q;
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++)
        {
            df[i].num=i;
            scanf("%d",&df[i].val);
            q.push(df[i]);
        }
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d %d",&x,&y);
            edge[x].push_back(y);
            edge[y].push_back(x);
        }
        ans=0;
        while(!q.empty())
        {
            int v=q.top().num;
            q.pop();
            for(int i=0;i<edge[v].size();i++)
            {
                if(vis[edge[v][i]]==0)
                    ans+=df[edge[v][i]].val;
            }
            vis[v]=1;
        }
        printf("%d\n",ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值