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.

样例输入
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的更多相关文章

  1. dp --- hdu 4939 : Stupid Tower Defense

    Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  2. hdu4939 Stupid Tower Defense (DP)

    2014多校7 第二水的题 4939 Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131 ...

  3. Stupid Tower Defense

    Problem Description FSF is addicted to a stupid tower defense game. The goal of tower defense games ...

  4. 初识Tower Defense Toolkit

    Tower Defense Toolkit 做塔防游戏的插件 主要层次如下图: 1GameControl _ _Game Control(Script) _ _ _Spawn Manager _ _ ...

  5. HDU4939Stupid Tower Defense (有思想的dp)

    Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...

  6. hdu 4779 Tower Defense (思维+组合数学)

    Tower Defense Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  7. HDU 4779:Tower Defense

    Tower Defense Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)T ...

  8. hihoCoder #1199 : Tower Defense Game ——(树型dp)

    题目链接:https://hihocoder.com/problemset/problem/1199. 题意:一棵以1为根的树,每个点有一个p值和q值,到这个点需要当前分数大于等于p,然后消耗掉(p- ...

  9. HDU 4939 Stupid Tower Defense(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4939 解题报告:一条长度为n的线路,路上的每个单元格可以部署三种塔来给走在这条路上的敌人造成伤害,第一 ...

随机推荐

  1. find the majority element

    Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to ...

  2. how to increase an regular array length in java?

    Arrays in Java are of fixed size that is specified when they are declared. To increase the size of t ...

  3. htop安装步骤【原创】

    htop安装步骤 下载:http://hisham.hm/htop/releases/ [root@hchtest2 ~]# tar zxvf htop-2.0.2.tar.gz [root@hcht ...

  4. linux 进程监控和自动重启的简单实现(转)

    目的:linux 下服务器程序会因为各种原因dump掉,就会影响用户使用,这里提供一个简单的进程监控和重启功能. 实现原理:由定时任务crontab调用脚本,脚本用ps检查进程是否存在,如果不存在则重 ...

  5. wpa_supplicant_8_ti hostapd wpa_supplicant TI 官方的wpa_supplicant hostapd 移植到linux

    在移植 wpa_supplicant_8_ti 的时候碰到很多头文件找不到.然后参考了下面的博客 http://blog.csdn.net/penglijiang/article/details/85 ...

  6. listener、context、filter、servlet及其加载顺序

    首先说加载顺序:context-param—>listener —> filter —> servlet 这四类加载顺序与配置顺序无关,对于每一类内部的加载顺序,与配置顺序有关: l ...

  7. 1.2 Python基本语法

    1.交互模式编程 cmd窗口   =>输入 Python => 输入 print "hello,python!";        ps:如果是新版本Python,需要输 ...

  8. Android Skia和2D图形系统 .

    Android Skia 和 2D 图形系统 1 Skia 概述 Skia 是 Google 一个底层的图形.图像.动画. SVG .文本等多方面的图形库,是 Android 中图形系统的引擎. Sk ...

  9. Jquery的parent和parents(找到某一特定的祖先元素)

    关于Jquery的parent和parents parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合.parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以 ...

  10. android下m、mm、mmm编译命令的使用

    android下m.mm.mmm编译命令的使用 通过查看android源码目录下的build/envsetup.sh文件,可知: - m:       Makes from the top of th ...