XTOJ 1252 Defense Tower【贪心】
来源:互联网 发布:数据标准化处理 编辑:程序博客网 时间:2024/06/05 00:46
Defense Tower
Accepted : 89 Submit : 172Time Limit : 3000 MS Memory Limit : 65536 KBDefense Tower
In ICPCCamp, there are
In the
Bobo would like to destroy all defense towers. When he tries to destroy the tower in city
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
The first line contains an integer
The second line contains
The
Output
For each set, an integer denotes the minimum total damage.
Sample Input
31 2 31 22 331 100 11 22 3
Sample Output
32
原题链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252
参考带博客:http://www.cnblogs.com/shuguangzw/p/5585781.html
思路:考虑每条边对玩家的伤害
假设连接的节点是u,v,破坏力是p[u]和p[v]
假设p[u]>p[v]
现在考虑u,v的删除顺序,如果先删u,这条边对玩家的伤害,是p[v],先删v,伤害是p[u]
所以显然对于每条边,我们都要先删权值大的,才能最好
怎么样才能对于每条边先删最大的呢,那就按照权值递减删就好了
所以 ret=Σ(min(p[u],p[v]))
复杂度O(n)
AC代码:
/** * 行有余力,则来刷题! * 博客链接:http://blog.csdn.net/hurmishine **/#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int maxn=1e5+10;int a[maxn];int main(){ //freopen("data.txt","r",stdin); int n; while(cin>>n) { for(int i=1; i<=n; i++) scanf("%d",&a[i]); int ans=0; int x,y; for(int i=1;i<n;i++) { scanf("%d%d",&x,&y); ans+=min(a[x],a[y]); } cout<<ans<<endl; } return 0;}
参考博客:http://blog.csdn.net/a852852852852/article/details/51720114
AC代码2:
/** * 行有余力,则来刷题! * 博客链接:http://blog.csdn.net/hurmishine **/#include <iostream>#include <cstdio>#include <vector>#include <queue>#include <cstring>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(){ //freopen("data.txt","r",stdin); 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); } return 0;}
- XTOJ 1252 Defense Tower【贪心】
- HDU 4939 Stupid Tower Defense(贪心+dp)
- HDU 4939 Stupid Tower Defense(dp+贪心)
- hihocoder Tower Defense Game(树上贪心)
- xtu oj 1252Defense Tower
- HDU 4939 Stupid Tower Defense(贪心+动态规划)
- 湘潭大学OJ Defense Tower(贪心/优先队列)
- Tower Defense 介绍
- hdu 4779 Tower Defense
- Stupid Tower Defense
- HDU4939 Stupid Tower Defense
- Hdu4939 Stupid Tower Defense
- hdu4939Stupid Tower Defense dp
- 【hihoCoder】Tower Defense Game
- hdoj4939Stupid Tower Defense【dp】
- 湘潭oj_1252 Defense Tower
- hiho一下 第109周 Tower Defense Game 树DP+贪心
- [AndEngine] GameSourceCode: Simple Tower Defense
- 第十三讲 文本,背景,边框不再单调
- 什么是嵌入式
- Arduino BMP085/BMP180气压传感器实验
- 【codevs 1003】电话连线
- PAT(A) - 1089. Insert or Merge (25)
- XTOJ 1252 Defense Tower【贪心】
- 第1组UI组件:布局管理器
- 【Linux4.1.12源码分析】协议栈gro收包之IP层处理
- gcc三级优化分别做了什么
- VMware虚拟机网络配置选项
- 打字游戏初级版——C语言
- 百度网盘爬虫
- Cookie/Session机制详解
- CURL命令介绍