博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tower Defense Game
阅读量:5032 次
发布时间:2019-06-12

本文共 3147 字,大约阅读时间需要 10 分钟。

Tower Defense Game

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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.

样例输入
42 14 32 12 11 21 33 4
样例输出
   5
分析:题目大意是给你一棵树,每个节点都有支出和收入,问你从1号根节点深搜完所有节点至少要带多少钱?
   假设你带的钱很多,那该怎么走呢?
   你到了某一结点,应该走其中一棵子树,使得你走完之后剩下的钱最多,这样必定是最优的;
   所以dfs回溯求解,最后回溯到1号节点就是答案;
代码:
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set
::iterator it=s.begin();it!=s.end();it++)#define vi vector
#define pii pair
#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+10;const int dis[4][2]={ { 0,1},{-1,0},{ 0,-1},{ 1,0}};using namespace std;using namespace __gnu_cxx;ll gcd(ll p,ll q){ return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){ if(q&1)f=f*p;p=p*p;q>>=1;}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]=1; 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,1,n)scanf("%d%d",&p[i],&q[i]); rep(i,1,n-1)scanf("%d%d",&j,&k),a[j].pb(k),a[k].pb(j); dfs(1); printf("%d\n",ans[1].fi); //system ("pause"); return 0;}

 

转载于:https://www.cnblogs.com/dyzll/p/5730834.html

你可能感兴趣的文章
POJ_3967_Ideal Path
查看>>
将Ubuntu下网卡名称enss改为eth0
查看>>
VS 里附加库目录的设置
查看>>
移动端jq及zepto事件绑定
查看>>
记五一清北(济南)
查看>>
Centos非管理员安装Python和pip
查看>>
切片器化繁为简,盘它 !
查看>>
Hdu 1181 变形课
查看>>
关于Unity中的3D拾取
查看>>
Link Maker 为 Apple Music、iTunes Store、App Store、iBooks Store 以及 Mac App Store 创建链接。...
查看>>
图的广度优先遍历补分
查看>>
String Boot-thymeleaf使用(四)
查看>>
吴裕雄 数据挖掘与分析案例实战(13)——GBDT模型的应用
查看>>
loj10159. 「一本通 5.2 练习 2」旅游规划
查看>>
SpringBoot(八) Caching
查看>>
[TYVJ1930]编年史
查看>>
【BZOJ 2916】 2916: [Poi1997]Monochromatic Triangles (容斥)
查看>>
js、ajax乱码
查看>>
【转载】回首大学四年,一个电工对大学课程的见解
查看>>
MySQL监控和预警
查看>>