Defense Tower | ||
Accepted : 45 | Submit : 85 | |
Time Limit : 3000 MS | Memory Limit : 65536 KB |
Defense TowerIn ICPCCamp, there are n cities and (n−1) (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. InputThe input contains at most 30 sets. For each set: The first line contains an integer n ( 1≤n≤10 5 ). The second line contains n integers p 1 ,p 2 ,…,p n ( 1≤p i ≤10 4 ). The i -th of the last (n−1) lines contains 2 integers a i ,b i ( 1≤a i ,b i ≤n ). OutputFor each set, an integer denotes the minimum total damage. Sample Input3 1 2 3 1 2 2 3 3 1 100 1 1 2 2 3 Sample Output3 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
找的思路。。。