uva 10604 Chemical Reaction (DP)
来源:互联网 发布:程序员面试自我介绍 编辑:程序博客网 时间:2024/05/19 13:56
Problem E
Chemical Reaction
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.
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 ‘.’ For 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.
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
-6
#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <vector>#define ll long longusing namespace std;const int maxn = 11;ll dp[maxn][maxn][maxn][maxn][maxn][maxn] , energy[maxn][maxn];bool vis[maxn][maxn][maxn][maxn][maxn][maxn];int f[maxn][maxn];int num[maxn];void initial(){memset(dp , -1 , sizeof dp);memset(vis , false , sizeof vis);for(int i = 0;i < maxn;i++){num[i] = 0;}}void readcase(){int m , k , x;scanf("%d" , &m);for(int i = 1;i <= m;i++){for(int j = 1;j <= m;j++){scanf("%d%lld" , &f[i][j] , &energy[i][j]);}}scanf("%d" , &k);for(int i = 0;i < k;i++){scanf("%d" , &x);num[x]++;}char c;cin >> c;}ll DP(){if(num[1]+num[2]+num[3]+num[4]+num[5]+num[6] == 1) return 0;if(vis[num[1]][num[2]][num[3]][num[4]][num[5]][num[6]]){return dp[num[1]][num[2]][num[3]][num[4]][num[5]][num[6]];}vis[num[1]][num[2]][num[3]][num[4]][num[5]][num[6]] = true;ll ans = 1e17;for(int i = 1;i <= 6;i++){for(int j = 1;j <= 6;j++){if(i != j && num[i] > 0 && num[j] > 0 || (i == j && num[i] > 1)){num[i]--;num[j]--;num[f[i][j]]++;ans = min(ans , DP()+energy[i][j]);num[i]++;num[j]++;num[f[i][j]]--;}}}return dp[num[1]][num[2]][num[3]][num[4]][num[5]][num[6]] = ans;}int main(){int t;scanf("%d" , &t);while(t--){initial();readcase();printf("%lld\n" , DP());}return 0;}
- uva 10604 Chemical Reaction (DP)
- Chemical Reaction - UVa 10604 dp
- dp(UVa 10604 - Chemical Reaction)
- uva 10604 Chemical Reaction
- UVA - 10604 Chemical Reaction
- uva 10604 - Chemical Reaction
- uva 10604 - Chemical Reaction
- UVA 10604 - Chemical Reaction
- UVA - 10604 Chemical Reaction
- UVA - 10604 Chemical Reaction
- uva 10604Chemical Reaction
- uva 10604 - Chemical Reaction(状态压缩dp)
- UVA 10604 - Chemical Reaction (状态压缩)
- UVA - 10604 Chemical Reaction 记忆化搜索
- uva 10604 Chemical Reaction 记忆化搜索
- UVA 10604--Chemical Reaction+记忆化搜索
- UVA - 10604 Chemical Reaction(6维dp+记忆化搜索)
- uva 10604 - Chemical Reaction (记忆化搜索)
- Android学习之路一
- Openstack-G版单机
- Qt210 大文件传输错误
- 自编码算法与稀疏性
- (15)Java网络编程-应用练习篇
- uva 10604 Chemical Reaction (DP)
- 两个孤独的灵魂
- 我的2013
- Linux C/C++无ide下的编译工具的使用
- 开始学习《windows核心编程》
- 小米wifi 共享盘 设置
- LeetCode Climbing Stairs
- Linux防火墙iptables学习笔记(一)入门要领
- Orale 大牛们的博客