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.
- 样例输入
-
42 14 32 12 11 21 33 4
样例输出
5
- 分析:题目大意是给你一棵树,每个节点都有支出和收入,问你从1号根节点深搜完所有节点至少要带多少钱? 假设你带的钱很多,那该怎么走呢? 你到了某一结点,应该走其中一棵子树,使得你走完之后剩下的钱最多,这样必定是最优的; 所以dfs回溯求解,最后回溯到1号节点就是答案; 代码:
#include#include #include #include #include #include #include #include #include #include