HDU 3696 - Farm Game (DFS)

题意

给出几种物品的转化关系,问最多能赚到多少钱

思路

对每个东西DFS,求出一单位的这个东西最多能换到多少钱,之后加起来。

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <cstring>
  11. #include <iomanip>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <string>
  15. #include <map>
  16. #include <cmath>
  17. using namespace std;
  18. #define LL long long
  19. #define ULL unsigned long long
  20. #define SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define X first
  26. #define Y second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. #define BitCountll(x) __builtin_popcountll(x)
  35. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  36. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  37. const double PI = acos(-1.0);
  38. const int INF = 0x3f3f3f3f;
  39. const double eps = 1e-8;
  40. const int MAXN = 10000 + 10;
  41. const int MOD = 1000007;
  42. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  43. int cases = 0;
  44. typedef pair<int, int> pii;
  45. typedef vector<int>::iterator viti;
  46. typedef vector<pii>::iterator vitii;
  47. vector<pair<int, double> > G[MAXN];
  48. pair<double, double> arr[MAXN];
  49. int n;
  50. double maxValue[MAXN];
  51. void Input()
  52. {
  53. for (int i = 1; i <= n; i++) scanf("%lf%lf", &arr[i].Y, &arr[i].X);
  54. int nn, T;
  55. scanf("%d", &T);
  56. double rate;
  57. for (int i = 0; i < T; i++)
  58. {
  59. int pre, next;
  60. scanf("%d%d", &nn, &pre);
  61. for (int j = 0; j < (nn<<1) - 2; j++)
  62. {
  63. if (!(j&1)) scanf("%lf", &rate);
  64. else
  65. {
  66. scanf("%d", &next);
  67. G[pre].PB(MP(next, rate));
  68. pre = next;
  69. }
  70. }
  71. }
  72. }
  73. double DFS(int cur)
  74. {
  75. if (maxValue[cur]) return maxValue[cur];
  76. double ret = arr[cur].Y;
  77. for (int i = 0; i < SZ(G[cur]); i++) ret = max(ret, G[cur][i].Y * DFS(G[cur][i].X));
  78. return maxValue[cur] = ret;
  79. }
  80. void Init()
  81. {
  82. MS(maxValue, 0);
  83. for (int i = 1; i <= n; i++) G[i].clear();
  84. }
  85. int main()
  86. {
  87. //ROP;
  88. int i, j;
  89. while (scanf("%d", &n), n)
  90. {
  91. Init();
  92. Input();
  93. for (i = 1; i <= n; i++) DFS(i);
  94. double ans = 0;
  95. for (i = 1; i <= n; i++) ans += arr[i].X * maxValue[i];
  96. printf("%.2f\n", ans);
  97. }
  98. return 0;
  99. }
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值