Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. /*
  2. * VJUDGE D - Aquarium
  3. * author: roy4801
  4. * (C++)
  5. */
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. #define PROB "D"
  11. #define TESTC ""
  12.  
  13. #define USE_CPPIO() ios_base::sync_with_stdio(0); cin.tie(0)
  14. typedef long long int LL;
  15. typedef unsigned long long ULL;
  16. typedef pair<int, int> P;
  17. typedef pair<LL, LL> PLL;
  18. #define F first
  19. #define S second
  20. #define INF 0x3f3f3f3f
  21. #define MP make_pair
  22. #define MT make_tuple
  23. #define PB push_back
  24. #define PPB pop_back
  25. #define PF push_front
  26. #define PPF pop_front
  27. #define L 100
  28. #define preS(x) ((x) - (c+1))
  29. #define preB(x) (preS(x)+1)
  30. char m[L+5][L+5];
  31. int w[L+5][L+5];
  32. int kase, C=1;
  33. int r, c; // i, j
  34. int ans;
  35. struct E
  36. {
  37. E(int a, int b, int wei)
  38. {
  39. this->a = a;
  40. this->b = b;
  41. this->wei = wei;
  42. }
  43. int a, b, wei;
  44. friend bool operator<(const E &lhs, const E &rhs)
  45. {
  46. return lhs.wei < rhs.wei;
  47. }
  48. };
  49. vector<E> e;
  50. struct disjoint
  51. {
  52. int p[100005];
  53. void init() {
  54. for(int i = 0; i <= 2*L+5; i++) p[i] = i;
  55. }
  56. int find(int x) {
  57. return p[x] == x ? x : (p[x] = find(p[x]));
  58. }
  59. void uni(int a, int b) {
  60. // printf(">> uni %d %d\n", a, b);
  61. p[find(a)] = find(b);
  62. }
  63. bool isSame(int a, int b) {
  64. return find(a) == find(b);
  65. }
  66. };
  67. disjoint d;
  68. int main()
  69. {
  70. #ifdef DBG
  71. freopen("./testdata/" PROB TESTC ".in", "r", stdin);
  72. freopen("./testdata/" PROB ".out", "w", stdout);
  73. #endif
  74. cin >> kase;
  75. while(kase-- && cin >> r >> c)
  76. {
  77. d.init();
  78. e.clear();
  79. ans = 0;
  80. //
  81. cin.get();
  82. for(int i = 0; i < r; i++)
  83. {
  84. for(int j = 0; j < c; j++)
  85. cin >> m[i][j];
  86. cin.get();
  87. }
  88. for(int i = 0; i < r; i++)
  89. for(int j = 0; j < c; j++)
  90. cin >> w[i][j];
  91. //
  92. int ii = 1; // current small number
  93. for(int i = 0; i < r; i++)
  94. {
  95. for(int j = 0; j < c; j++)
  96. {
  97. // printf("> cur %d %d %d %d\n", ii, ii+1, preS(ii), preB(ii));
  98. e.PB(E(ii, ii+1, w[i][j]));
  99. if(i > 0)
  100. {
  101. if(m[i-1][j] == '/')
  102. {
  103. if(m[i][j] == '/')
  104. e.PB(E(ii, preB(ii), 0));
  105. else
  106. e.PB(E(ii+1, preB(ii), 0));
  107. }
  108. else /* \ */
  109. {
  110. if(m[i][j] == '/')
  111. e.PB(E(ii, preS(ii), 0));
  112. else
  113. e.PB(E(ii+1, preS(ii), 0));
  114. }
  115. }
  116. ii++;
  117. }
  118. ii++;
  119. }
  120. sort(e.begin(), e.end());
  121.  
  122. // for(auto &i : e)
  123. // printf("> %d %d %d\n", i.a, i.b, i.wei);
  124.  
  125. for(auto &i : e)
  126. {
  127. if(!d.isSame(i.a, i.b))
  128. {
  129. ans += i.wei;
  130. d.uni(i.a, i.b);
  131. }
  132. }
  133. printf("Case %d: %d\n", C++, ans);
  134. }
  135.  
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement