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的线路,路上的每个单元格可以部署三种塔来给走在这条路上的敌人造成伤害,第一 ...
随机推荐
- 桥接 NAT HOST-ONLY
无论是vmware,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3.Host-Only 桥接 桥接网络是指本地物理网卡和虚拟网卡通 ...
- caffe的matlab接口一览表
blob 简述 方法: shape reshape get_diff set_diff 私有方法: check_and_preprocess_shape check_and_preprocess_da ...
- Ansible10:Playbook的角色与包含【转】
当单个playbook文件越来越大的时候,我们就需要重新来组织Playbooks了.我们可以将一个大的playbook拆成若干个小的playbook文件,然后通过include的方式,在主配置文件中将 ...
- 解决scrollview不滚动
scrollView不滚动的时候 试试这个,有时候药到病除:
- linq中的分组和排序
一.分组 group 组内成员 by 分组条件 into 组的信息 class Program { static void Main(string[] args) { string[] name = ...
- asp之FSO大全
<%Function ShowDriveInfo(strFolder)'显示磁盘信息'strRootFolder="/"'strDrivInfo=ShowDriveInfo( ...
- WPF之DataGrid的LoadingRow事件
利用DataGrid的LoadingRow事件,可以进行设置DataGrid的DataGridRow的属性(例如样式:背景色.前景色:是否可修改数据) 实例代码如下: private void Dat ...
- ExceL转PDF
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = ...
- ASP MVC之参数传递
1.URL获取参数 Request.QueryString["XqType"]; 2.表单提交,control层获取参数:Request.Form["XXX" ...
- iOS多线程中performSelector
下面两段代码都在主线程中运行,我们在看别人代码时会发现有时会直接调用,有时会利用performSelector调用,今天看到有人在问这个问题,我便做一下总结, [delegate imageDownl ...