Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <string>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <vector>
  7. #include <map>
  8. #include <stack>
  9. #define maxn 2000005
  10. using namespace std;
  11. int b, m;
  12. int dish[maxn];
  13. int cost[maxn], val[maxn];
  14.  
  15. struct Edge {int to, w, val;};
  16. vector <Edge> edges;
  17. vector<int> G[maxn];
  18. int indeg[maxn];
  19. map<string, int> id;
  20. stack<int> s;
  21.  
  22. int f[maxn];
  23. vector<int> T;
  24.  
  25. int tot=0;
  26. int getid(string s) {
  27. if (id.count(s))
  28. return id[s];
  29. id[s] = ++tot;
  30. // cout << endl << s << " == " << tot;
  31. return tot;
  32. }
  33.  
  34. int main() {
  35. cin.tie(0);
  36. ios_base::sync_with_stdio(false);
  37. cin >> b;cin >> m;
  38.  
  39. string s1, s2, tmp;
  40. int tmpc, tmpv;
  41. while (m--) {
  42. cin >> s1 >> s2 >> tmp;
  43.  
  44. scanf("%d%d", &tmpc, &tmpv);
  45. int id1 = getid(s1), id2 = getid(s2);
  46. edges.push_back((Edge){id1, tmpc, tmpv});
  47. G[id2].push_back(edges.size()-1);
  48. indeg[id1]++;
  49. }
  50. // puts("OK");
  51. memset(cost, 63, sizeof(cost));
  52. for (int i=1; i<=tot; i++) {
  53. if (!indeg[i]) {
  54. s.push(i);
  55. // printf(" start i %d\n", i);
  56. cost[i] = val[i] = 0;
  57. }
  58. }
  59.  
  60. while (!s.empty()) {
  61. int u = s.top(); s.pop();
  62. // printf("u == %d\n", u);
  63. dish[cost[u]] = max(dish[cost[u]], val[u]);
  64. for (int i=0; i<G[u].size(); i++) {
  65. Edge& e = edges[G[u][i]];
  66. int v = e.to;
  67. // printf("from %d -- to %d\n", u, v);
  68. if (cost[v] > cost[u] + e.w) {
  69. cost[v] = cost[u] + e.w;
  70. val[v] = val[u] + e.val;
  71. }
  72. else if (cost[v] == cost[u] + e.w)
  73. val[v] = max(val[v], val[u] + e.val);
  74.  
  75. indeg[v]--;
  76. if (!indeg[v])
  77. s.push(v);
  78. }
  79. }
  80.  
  81. for (int i=0; i<=b; i++) {
  82. for (int v = b; v >=0; v--) {
  83. if (v - i >= 0) {
  84. f[v] = max(f[v], f[v-i]+dish[i]);
  85. }
  86. }
  87. }
  88. int Ans1 = 0, Ans2 = 0;
  89. for (int v=0; v<=b; v++) {
  90. if (f[v] > Ans1) {
  91. Ans1 = f[v];
  92. Ans2 = v;
  93. }
  94. }
  95. printf("%d\n%d\n", Ans1, Ans2);
  96.  
  97. return 0;
  98.  
  99.  
  100. }
  101.  
  102. /*
  103.  
  104.  
  105.  
  106. 15
  107. 6
  108. pizza_tomato pizza_base tomato 1 2
  109. pizza_cheese pizza_base cheese 5 10
  110. pizza_classic pizza_tomato cheese 5 5
  111. pizza_classic pizza_cheese tomato 1 2
  112. pizza_salami pizza_classic salami 7 6
  113. pizza_spicy pizza_tomato chili 3 1
  114.  
  115. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement