UVA 10604 (记忆化搜索 + hash)
2013-10-25 22:22
471 查看
In a chemist’s lab, there are several types of chemicals in tubes. The chemist wants to mix all these chemicals together, two chemicals at a time. Whenever two chemicals are mixed, some heat is generated and released into the air and the mixed chemical is
a known chemical of possibly other type than the original two. The resulting chemical type and the amount of heats emitted can looked up in the chemical mixture table.
For example, in the above chemical mixture table, there are three types of chemicals: 1, 2, and 3. If you mix chemicals 1 and 3, they produce +3000 units of heat and turn into chemical 3. Sometimes, the heat generated can be negative. For instance, you can
mix 2 and 3 and they turn into chemical 1 and in the meantime, cool down the lab by 500 units of heat. Since the chemist lacks funding to buy necessary equipments to protect himself from the heat generated, it is utmost important to find a way to mix all the
chemicals together that produces the least total heat, regardless of the final chemical type. For example, suppose the lab has four tubes containing chemicals of types 1, 2, 2, and 3. If the chemicals are mixed in the parenthesize order of ((1 2) (2 3)), it
will produce (–10)+ (-500)+(3000) = 2490 units of heat. However, if the chemicals are mixed in the (2 (1 (2 3))) order, it will produce (-500)+0+(-10)= -510 units of heat, which is also the least total heat possible.
each test case, the first line contains an integer number m (1≤m≤6) denoting the number chemical types. Therefore, the chemicals are indexed from 1 up to at most 6. The next mxm lines give the chemical mixture table entries in the row-major order. Each line
contains the new resulting chemical type and the amount of energy emitted. After the table entries is a line with a single number k (2≤k≤10) denoting number of tubes in the lab. The next line then contains k integers in the range of 1 and m, separated by blank
spaces, denoting the types of chemicals in those k tubes.
题意:给定n个试剂,一下n*n行表示每种试剂相加之后生成物和放吸热,然后在输入m,表示m种试剂,求混合方案使得放热最少。
思路:记忆化搜索,本来是开6维数组来记录状态结果超时了,看了大神代码发现改成hash来记录状态就过了。
代码:
a known chemical of possibly other type than the original two. The resulting chemical type and the amount of heats emitted can looked up in the chemical mixture table.
Chemicals | 1 | 2 | 3 | |||
Resulting chemical type | Heats emmited | Resulting chemical type | Heats emmited | Resulting chemical type | Heats emmited | |
1 | 1 | 0 | 3 | -10 | 3 | 3000 |
2 | 3 | -10 | 2 | 0 | 1 | -500 |
3 | 3 | 3000 | 1 | -500 | 3 | 0 |
For example, in the above chemical mixture table, there are three types of chemicals: 1, 2, and 3. If you mix chemicals 1 and 3, they produce +3000 units of heat and turn into chemical 3. Sometimes, the heat generated can be negative. For instance, you can
mix 2 and 3 and they turn into chemical 1 and in the meantime, cool down the lab by 500 units of heat. Since the chemist lacks funding to buy necessary equipments to protect himself from the heat generated, it is utmost important to find a way to mix all the
chemicals together that produces the least total heat, regardless of the final chemical type. For example, suppose the lab has four tubes containing chemicals of types 1, 2, 2, and 3. If the chemicals are mixed in the parenthesize order of ((1 2) (2 3)), it
will produce (–10)+ (-500)+(3000) = 2490 units of heat. However, if the chemicals are mixed in the (2 (1 (2 3))) order, it will produce (-500)+0+(-10)= -510 units of heat, which is also the least total heat possible.
Input
The first line of input file consists of a single number denoting the number of test cases in the file. There is a single line containing a ‘/’ character separating two consecutive test cases. The end of the file is marked with a line containing a ‘.’ Foreach test case, the first line contains an integer number m (1≤m≤6) denoting the number chemical types. Therefore, the chemicals are indexed from 1 up to at most 6. The next mxm lines give the chemical mixture table entries in the row-major order. Each line
contains the new resulting chemical type and the amount of energy emitted. After the table entries is a line with a single number k (2≤k≤10) denoting number of tubes in the lab. The next line then contains k integers in the range of 1 and m, separated by blank
spaces, denoting the types of chemicals in those k tubes.
Output
For each test case, output the least total heat possible on a single line.Sample Input | Sample Output |
2 3 1 0 3 -10 3 3000 3 -10 2 0 1 -500 3 3000 1 -500 3 0 4 1 2 2 3 / 3 1 0 3 500 3 -250 3 500 2 0 1 100 3 -250 1 100 3 0 6 1 1 1 2 2 3 . | -510 -650 |
思路:记忆化搜索,本来是开6维数组来记录状态结果超时了,看了大神代码发现改成hash来记录状态就过了。
代码:
#include <stdio.h> #include <string.h> const int N = 15; int min(int a, int b) {return a < b ? a : b;} int t, n, m, ans, h , num , c , r , i, j, dp[2000000], vis[2000000]; char s[10]; int hash() { return 1000000 + h[1] * 100000 + h[2] * 10000 + h[3] * 1000 + h[4] * 100 + h[5] * 10 + h[6] * 1; } int dfs(int num) { int i, j; int x = hash(); if (vis[x]) return dp[x]; int &ans = dp[x]; ans = 999999999; if (num == m - 1) ans = 0; for (i = 1; i <= n; i ++) { if (!h[i]) continue; h[i] --; for (j = 1; j <= n; j ++) { if (!h[j]) continue; h[j] --; h[c[i][j]] ++; ans = min(ans, dfs(num + 1) + r[i][j]); h[c[i][j]] --; h[j] ++; } h[i] ++; } vis[x] = 1; return ans; } int main() { scanf("%d", &t); while (t --) { memset(vis, 0, sizeof(vis)); memset(dp, 0, sizeof(dp)); memset(h, 0, sizeof(h)); scanf("%d", &n); for (i = 1; i <= n; i ++) for (j = 1; j <= n; j ++) { scanf("%d%d", &c[i][j], &r[i][j]); } scanf("%d", &m); int z; for (i = 1; i <= m; i ++) { scanf("%d", &z); h[z] ++; } printf("%d\n", dfs(0)); scanf("%s", s); } return 0; }
相关文章推荐
- UVa 10604 Chemical Reaction(hash记忆化搜索)
- UVA - 10604 Chemical Reaction 记忆化搜索
- UVA - 10604Chemical Reaction(记忆化搜索)
- uva 10604 - Chemical Reaction (记忆化搜索)
- uva 10604 Chemical Reaction 记忆化搜索
- uva_10604 - Chemical Reaction( Hash+状态压缩 )
- UVALive 3710 Interconnect(记忆化搜索 + hash)
- uva 141 The Spot Game hash
- UVA 1629 Cake slicing——记忆化搜索
- The Tower of Babylon UVA - 437 (DAG上的动态规划变形,记忆化搜索)
- uva1637(记忆化搜索)
- UVA 261 - The Window Property(字符串Hash)
- UVALive4671:K-neighbor substrings (FFT+Hash)
- UVA 10859 Placing Lampposts(记忆化搜索)
- UVALive - 6893 The Big Painting (BKDRhash扩展到二维矩阵)
- uva 188 Perfect Hash(进制转换 + 遍历)
- UVA 1629 - Cake slicing(记忆化搜索)
- hash+状态压缩+uva10944
- UVALive 4513 Stammering Aliens 字符串Hash求LCS
- uva 707(记忆化搜索)