nhap96

UVa 1235

May 9th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /* ---------------------------------------------------------------------------------------- */
  2.  
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <cstdlib>
  7. #include <climits>
  8. #include <cstring>
  9. #include <iomanip>
  10. #include <limits>
  11. #include <string>
  12. #include <locale>
  13. #include <cstdio>
  14. #include <vector>
  15. #include <cmath>
  16. #include <stack>
  17. #include <queue>
  18. #include <deque>
  19. #include <ctime>
  20. #include <map>
  21. #include <set>
  22.  
  23. using namespace std;
  24.  
  25. #define fi first
  26. #define se second
  27. #define mp make_pair
  28. #define pb push_back
  29.  
  30. #define rp(i, n) for (int i = 0; i < (n); ++i)
  31. #define rd(i, n) for (int i = (n); i--;)
  32. #define rs(i, x) rp (i, sz (x))
  33. #define fr(i, a, b) for (int i = (a); i <= (b); ++i)
  34. #define fd(i, a, b) for (int i = (a); i >= (b); --i)
  35. #define fe(i, x) for (__typeof ((x).begin ()) i = (x).begin (); i != (x).end (); ++i)
  36. #define fer(i, x) for (__typeof ((x).rbegin ()) i = (x).rbegin (); i != (x).rend (); ++i)
  37. #define cd(x) while ((x)--)
  38. #define nt(n) for (int i = (n); i--;)
  39. #define srt(v) sort (all (v))
  40.  
  41. #define mn(x, y) x = min (x, y)
  42. #define mx(x, y) x = max (x, y)
  43.  
  44. #define sz(x) (int) (x).size ()
  45. #define all(x) (x).begin (), (x).end ()
  46.  
  47. #define cl(x) memset (x, 0, sizeof (x))
  48.  
  49. #define sqr(x) ((x) * (x))
  50.  
  51. const double pi = acos(-1.0);
  52.  
  53. typedef unsigned long long llu;
  54. typedef long long ll;
  55.  
  56. typedef pair <int, int> ii;
  57. typedef vector <string> vs;
  58. typedef vector <ii> vii;
  59. typedef vector <int> vi;
  60. typedef vector <vi> vvi;
  61. typedef vector <vii> vvii;
  62. typedef vector <bool> vb;
  63. typedef vector <vb> vvb;
  64.  
  65. template <class T>
  66. inline string ns (const T &number)
  67. {
  68. stringstream ss;
  69. ss << number;
  70. return ss.str ();
  71. }
  72.  
  73. template <class T>
  74. inline T sn (const string &text)
  75. {
  76. stringstream ss (text);
  77. T result;
  78. return ss >> result ? result : 0;
  79. }
  80.  
  81. /* ---------------------------------------------------------------------------------------- */
  82.  
  83. vi pSet;
  84.  
  85. void init (const int &n)
  86. {
  87. pSet.resize (n);
  88. rp (i, n) pSet [i] = i;
  89. }
  90.  
  91. int find (const int &x)
  92. {
  93. return pSet [x] == x ? x : pSet [x] = find (pSet [x]);
  94. }
  95.  
  96. inline bool unionSet (const int &x, const int &y)
  97. {
  98. int xRoot = find (x), yRoot = find (y);
  99. if (xRoot != yRoot)
  100. {
  101. pSet [yRoot] = xRoot;
  102. return true;
  103. }
  104. return false;
  105. }
  106.  
  107. int main ()
  108. {
  109. srand (time (NULL));
  110. #ifndef ONLINE_JUDGE
  111. freopen ("1235.inp", "r", stdin);
  112. freopen ("1235.check", "w", stdout);
  113. //freopen ("err.txt", "w", stderr);
  114. #endif
  115. int t; cin >> t;
  116. cd (t)
  117. {
  118. int n; cin >> n;
  119. vs key (n);
  120. typedef pair <int, pair <int, int> > cost;
  121. priority_queue <cost, vector <cost>, greater <cost> > pq;
  122. int res = 100;
  123.  
  124. rp (i, n)
  125. {
  126. cin >> key [i];
  127. int tmp = 0;
  128. rp (j, 4)
  129. {
  130. key [i][j] -= '0';
  131. tmp += min <int> (key [i][j], 10 - key [i][j]);
  132. }
  133. mn (res, tmp);
  134. rp (j, i)
  135. {
  136. tmp = 0;
  137. rp (k, 4) tmp += min ((10 + key [i][k] - key [j][k]) % 10, (10 + key [j][k] - key [i][k]) % 10);
  138. pq.push (mp (tmp, mp (i, j)));
  139. }
  140. }
  141.  
  142. init (n);
  143. int cnt = n - 1;
  144. while (cnt)
  145. {
  146. cost tmp = pq.top ();
  147. pq.pop ();
  148. if (unionSet (tmp.se.fi, tmp.se.se))
  149. {
  150. --cnt;
  151. res += tmp.fi;
  152. }
  153. }
  154. cout << res << endl;
  155. }
  156. return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment