Tower Defense Game
Tower Defense Game
描述
There is a tower defense game with n levels(missions). The n levels form a tree whose root is level 1.
In the i-th level, you have to spend pi units of money buying towers and after the level, you can sell the towers so that you have qi units of money back.

Each level is played once. You can choose any order to complete all the levels, but the order should follow the following rules:
1: A level can be played when all its ancestors are completed.
2: The levels which form a subtree of the original tree should be completed consecutively in your order.
You want to know in which order of completion you can bring the minimum money before starting the first level.
输入
The first line contains one single integers, n - the number of levels. (1<=n<=10000)
The next n lines describes the levels. Each line contains 2 integers, pi and qi which are described in the statement. (0<=qi<=pi<=20000)
The next n-1 lines describes the tree. Each line contains 2 integers, ai and bi which means there is an edge between level ai and level bi.
For 30% of the data, n<=100.
For 60% of the data, n<=1000.
输出
Print one line with an single integer representing the minimum cost.
样例提示
There are two orders of completing all levels which are: 1234 and 1342.
In the order 1234, the minimum beginning money is 5.
In the order 1342, the minimum beginning money is 7.
1324 is not a valid order because level 3 and level 4 are not completed consecutively.
- 样例输入
-
4
2 1
4 3
2 1
2 1
1 2
1 3
3 4 - 样例输出
5
- 分析:题目大意是给你一棵树,每个节点都有支出和收入,问你从1号根节点深搜完所有节点至少要带多少钱?
- 假设你带的钱很多,那该怎么走呢?
- 你到了某一结点,应该走其中一棵子树,使得你走完之后剩下的钱最多,这样必定是最优的;
- 所以dfs回溯求解,最后回溯到1号节点就是答案;
- 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e4+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,p[maxn],q[maxn],vis[maxn];
vi a[maxn];
pii ans[maxn];
bool cmp(const int&x,const int&y)
{
return ans[x].se>ans[y].se;
}
pii dfs(int s)
{
vis[s]=;
vi son;
ans[s]={p[s],q[s]};
for(int x:a[s])if(!vis[x])ans[x]=dfs(x),son.pb(x);
sort(son.begin(),son.end(),cmp);
for(int x:son)
{
if(ans[x].fi>ans[s].se)ans[s].fi+=ans[x].fi-ans[s].se,ans[s].se=ans[x].se;
else ans[s].se+=ans[x].se-ans[x].fi;
}
return ans[s];
}
int main()
{
int i,j,k,t;
scanf("%d",&n);
rep(i,,n)scanf("%d%d",&p[i],&q[i]);
rep(i,,n-)scanf("%d%d",&j,&k),a[j].pb(k),a[k].pb(j);
dfs();
printf("%d\n",ans[].fi);
//system ("pause");
return ;
}
Tower Defense Game的更多相关文章
- dp --- hdu 4939 : Stupid Tower Defense
Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- hdu4939 Stupid Tower Defense (DP)
2014多校7 第二水的题 4939 Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131 ...
- Stupid Tower Defense
Problem Description FSF is addicted to a stupid tower defense game. The goal of tower defense games ...
- 初识Tower Defense Toolkit
Tower Defense Toolkit 做塔防游戏的插件 主要层次如下图: 1GameControl _ _Game Control(Script) _ _ _Spawn Manager _ _ ...
- HDU4939Stupid Tower Defense (有思想的dp)
Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- hdu 4779 Tower Defense (思维+组合数学)
Tower Defense Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- HDU 4779:Tower Defense
Tower Defense Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)T ...
- hihoCoder #1199 : Tower Defense Game ——(树型dp)
题目链接:https://hihocoder.com/problemset/problem/1199. 题意:一棵以1为根的树,每个点有一个p值和q值,到这个点需要当前分数大于等于p,然后消耗掉(p- ...
- HDU 4939 Stupid Tower Defense(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4939 解题报告:一条长度为n的线路,路上的每个单元格可以部署三种塔来给走在这条路上的敌人造成伤害,第一 ...
随机推荐
- swap in java?
Is it possible to write a swap method in java? these two variables will be primitives. It's not poss ...
- MyEclipse修改项目名称
项目上右击鼠标–>Properties–>MyEclipse–>Web–>Content Root–>Web content-root–>改成你想要的项目名 即可 ...
- 在Linux中设置共享目录
1. 是否安装了samba服务 sudo apt-get install samba sudo apt-get install smbfs 2. 创建需要共享的目录/ home/usr/share ...
- 解决Sublime Text 3中文显示乱码(tab中文方块)问题,sublime tab乱码
一.文本出现中文乱码问题 1.打开Sublime Text 3,按Ctrl+-打开控制行,复制粘贴以下python代码,然后回车运行. 2. 复制并粘贴如下代码: import urllib.requ ...
- C、C++的Makefile的编写以及动、静态库的制作调用(包括MAC地址的获取及MD5加密)
一.C代码 静态库 四个.h.c文件 add.h #ifndef ADD_H #define ADD_H int add(int a,int b); #endif add.c #include < ...
- webwork <ww:if> 标签的使用
如果在前台(JSP)取出后台的对象的属性,这个属性在后台是属于String 类型的,但若这个属性的值为数字,取出在前台就会默认为整形的值,所以在<ww:if> 判断里面不能加引号:< ...
- curl 测试web站点的响应时间
curl -s -w "\n"::%{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total} ...
- Content-Type小解
在Http请求中,经常用Content-Type来定义网络文件的类型和网页的编码,在发送请求,返回数据时决定浏览器将以什么形式,什么编码来读取此文件. 常用类型: text 文本类型 1.text/p ...
- 为ubuntu添加右键打开终端效果
安装工具:nautilus-open-terminal sudo apt-get install nautilus-open-terminal 注销或重启
- PostConstruct注解
应用场景:当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时 public class UserDaoImpl extends HibernateDao ...