湘潭大学OJ Defense Tower(贪心/优先队列)
2016-06-12 20:33
232 查看
Defense Tower | ||
Accepted : 45 | Submit : 85 | |
Time Limit : 3000 MS | Memory Limit : 65536 KB |
Defense TowerIn ICPCCamp, there are ncities 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 most30 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
找的思路。。。
相关文章推荐
- AngularJS ui-view 嵌套及切换--index
- 错误解决
- jQuery-语法
- GDB的使用以及使用gdb来调试caffe代码
- js实现四叉树算法
- Jquery-隔行换色
- ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT 的解决办法
- Angular Js pdf文件下载
- 解决jsp参数传递乱码的问题
- js常用返回网页顶部几种方法
- 近期学习css+javascript
- jquery的each循环问题
- HTML最新标准HTML5小结
- CSS文字样式
- windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码
- Bootstrap网格系统
- Bootstrap表单按钮图标
- POJ 1543 Perfect Cubes
- Bootstrap排版
- Bootstrap排版