POJ 1789 Truck History

阳光穿透心脏的1/2处 2021-11-01 06:18 305阅读 0赞

题目链接:https://vjudge.net/problem/POJ-1789

题目大意

  给定 N 辆车的车牌号,每个车牌号由 7 个字母组成,每个车牌号都可以由另一个车牌号衍生而来,代价为两字符串具有不同的字符的位置数。现在要你构造一个衍生网络,使得车牌衍生总代价最小。

分析

  最小生成树模板题,由于此图为稠密图,应用 Prim 算法。

代码如下

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include <cmath>
  2. 2 #include <ctime>
  3. 3 #include <iostream>
  4. 4 #include <string>
  5. 5 #include <vector>
  6. 6 #include <cstdio>
  7. 7 #include <cstdlib>
  8. 8 #include <cstring>
  9. 9 #include <queue>
  10. 10 #include <map>
  11. 11 #include <set>
  12. 12 #include <algorithm>
  13. 13 #include <cctype>
  14. 14 #include <stack>
  15. 15 #include <deque>
  16. 16 #include <list>
  17. 17 #include <sstream>
  18. 18 #include <cassert>
  19. 19 using namespace std;
  20. 20
  21. 21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  22. 22 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  23. 23 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  24. 24 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  25. 25 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  26. 26 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  27. 27 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  28. 28 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  29. 29
  30. 30 #define pr(x) cout << #x << " = " << x << " "
  31. 31 #define prln(x) cout << #x << " = " << x << endl
  32. 32
  33. 33 #define LOWBIT(x) ((x)&(-x))
  34. 34
  35. 35 #define ALL(x) x.begin(),x.end()
  36. 36 #define INS(x) inserter(x,x.begin())
  37. 37 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  38. 38 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
  39. 39 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  40. 40 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  41. 41
  42. 42 #define ms0(a) memset(a,0,sizeof(a))
  43. 43 #define msI(a) memset(a,0x3f,sizeof(a))
  44. 44 #define msM(a) memset(a,-1,sizeof(a))
  45. 45
  46. 46 #define MP make_pair
  47. 47 #define PB push_back
  48. 48 #define ft first
  49. 49 #define sd second
  50. 50
  51. 51 template<typename T1, typename T2>
  52. 52 istream &operator>>(istream &in, pair<T1, T2> &p) {
  53. 53 in >> p.first >> p.second;
  54. 54 return in;
  55. 55 }
  56. 56
  57. 57 template<typename T>
  58. 58 istream &operator>>(istream &in, vector<T> &v) {
  59. 59 for (auto &x: v)
  60. 60 in >> x;
  61. 61 return in;
  62. 62 }
  63. 63
  64. 64 template<typename T1, typename T2>
  65. 65 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  66. 66 out << "[" << p.first << ", " << p.second << "]" << "\n";
  67. 67 return out;
  68. 68 }
  69. 69
  70. 70 inline int gc(){
  71. 71 static const int BUF = 1e7;
  72. 72 static char buf[BUF], *bg = buf + BUF, *ed = bg;
  73. 73
  74. 74 if(bg == ed) fread(bg = buf, 1, BUF, stdin);
  75. 75 return *bg++;
  76. 76 }
  77. 77
  78. 78 inline int ri(){
  79. 79 int x = 0, f = 1, c = gc();
  80. 80 for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
  81. 81 for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
  82. 82 return x*f;
  83. 83 }
  84. 84
  85. 85 template<class T>
  86. 86 inline string toString(T x) {
  87. 87 ostringstream sout;
  88. 88 sout << x;
  89. 89 return sout.str();
  90. 90 }
  91. 91
  92. 92 inline int toInt(string s) {
  93. 93 int v;
  94. 94 istringstream sin(s);
  95. 95 sin >> v;
  96. 96 return v;
  97. 97 }
  98. 98
  99. 99 //min <= aim <= max
  100. 100 template<typename T>
  101. 101 inline bool BETWEEN(const T aim, const T min, const T max) {
  102. 102 return min <= aim && aim <= max;
  103. 103 }
  104. 104
  105. 105 typedef long long LL;
  106. 106 typedef unsigned long long uLL;
  107. 107 typedef pair< double, double > PDD;
  108. 108 typedef pair< int, int > PII;
  109. 109 typedef pair< int, PII > PIPII;
  110. 110 typedef pair< string, int > PSI;
  111. 111 typedef pair< int, PSI > PIPSI;
  112. 112 typedef set< int > SI;
  113. 113 typedef set< PII > SPII;
  114. 114 typedef vector< int > VI;
  115. 115 typedef vector< double > VD;
  116. 116 typedef vector< VI > VVI;
  117. 117 typedef vector< SI > VSI;
  118. 118 typedef vector< PII > VPII;
  119. 119 typedef map< int, int > MII;
  120. 120 typedef map< LL, int > MLLI;
  121. 121 typedef map< int, string > MIS;
  122. 122 typedef map< int, PII > MIPII;
  123. 123 typedef map< PII, int > MPIII;
  124. 124 typedef map< string, int > MSI;
  125. 125 typedef map< string, string > MSS;
  126. 126 typedef map< PII, string > MPIIS;
  127. 127 typedef map< PII, PII > MPIIPII;
  128. 128 typedef multimap< int, int > MMII;
  129. 129 typedef multimap< string, int > MMSI;
  130. 130 //typedef unordered_map< int, int > uMII;
  131. 131 typedef pair< LL, LL > PLL;
  132. 132 typedef vector< LL > VL;
  133. 133 typedef vector< VL > VVL;
  134. 134 typedef priority_queue< int > PQIMax;
  135. 135 typedef priority_queue< int, VI, greater< int > > PQIMin;
  136. 136 const double EPS = 1e-8;
  137. 137 const LL inf = 0x3fffffff;
  138. 138 const LL infLL = 0x3fffffffffffffffLL;
  139. 139 const LL mod = 1e9 + 7;
  140. 140 const int maxN = 2e3 + 7;
  141. 141 const LL ONE = 1;
  142. 142 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  143. 143 const LL oddBits = 0x5555555555555555;
  144. 144
  145. 145 int N, M, ans;
  146. 146 int adj[maxN][maxN], dist[maxN];
  147. 147 string truck[maxN];
  148. 148 bool vis[maxN];
  149. 149
  150. 150 int calDist(string &s1, string &s2) {
  151. 151 int ret = 0;
  152. 152 Rep(i, s1.size()) if(s1[i] != s2[i]) ++ret;
  153. 153 return ret;
  154. 154 }
  155. 155
  156. 156 void Prim(int S) {
  157. 157 ms0(vis);
  158. 158 vis[S] = 1;
  159. 159 For(i, 1, N) dist[i] = adj[S][i];
  160. 160
  161. 161 Rep(cnt, N - 1) {
  162. 162 int index, minW = inf;
  163. 163
  164. 164 For(i, 1, N) {
  165. 165 if(!vis[i] && minW > dist[i]) {
  166. 166 index = i;
  167. 167 minW = dist[i];
  168. 168 }
  169. 169 }
  170. 170
  171. 171 ans += minW;
  172. 172 vis[index] = 1;
  173. 173
  174. 174 For(i, 1, N) if(!vis[i] && dist[i] > adj[index][i]) dist[i] = adj[index][i];
  175. 175 }
  176. 176 }
  177. 177
  178. 178 int main(){
  179. 179 //freopen("MyOutput.txt","w",stdout);
  180. 180 //freopen("input.txt","r",stdin);
  181. 181 INIT();
  182. 182 while(cin >> N && N) {
  183. 183 ans = 0;
  184. 184 For(i, 1, N) cin >> truck[i];
  185. 185 For(i, 1, N) {
  186. 186 For(j, i + 1, N) {
  187. 187 adj[i][j] = adj[j][i] = calDist(truck[i], truck[j]);
  188. 188 }
  189. 189 }
  190. 190
  191. 191 Prim(1);
  192. 192 if(ans == 1) cout << "The highest possible quality is 1." << endl;
  193. 193 else cout << "The highest possible quality is 1/" << ans << ".\n";
  194. 194 }
  195. 195 return 0;
  196. 196 }

转载于:https://www.cnblogs.com/zaq19970105/p/11303103.html

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读