UVA 10604 Chemical Reaction

Source: Internet
Author: User
Tags mixed

Original question:
In a chemists lab, there is several types of chemicals in tubes. The chemist wants to mix all these chemicals together, and both chemicals at a time. Whenever chemicals is mixed, some heat was generated and released into the air and the mixed chemical is a known Chemi Cal of possibly other type than the original. 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 is 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 to chemical 1 and in the meantime, cool off the lab by units of Hea T. Since the chemist lacks funding to buy necessary equipments to protect himself from the heat generated, it's utmost IM Portant to find a-the-mix all the chemicals together that produces the least total heat, regardless of the final chemic Al type. For example, suppose the lab had four tubes containing chemicals of types 1, 2, 2, and 3. If The chemicals is mixed in the parenthesize Order of (()), it would produce (−10) + (−500) + (in) = 2490 units O F Heat. However, if the chemicals is mixed in the (2 (1)) order, it would 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 the test cases in the file. There is a single line containing a '/' character separating, 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 is 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 are 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, the output of the least total heat possible to a single line.



Sample Input
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 10
3-250
1 100
3 0
6
1 1 1 2 2 3
.
Sample Output
-510
-740



English:
Give you a bunch of drugs, and each of these two drugs can be mixed to produce another medicine, and it emits heat. Now give you a list of medicines, and then give you a bunch of drugs. Ask you how to mix the heat released after the least.
(Note that a drug and B drugs mixed with b drugs and a drug mixed results will be different, reference Sample II) Note that the second result of the sample data is wrong: The answer should be -740 .


#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;
const int inf = 99999999;
int m, k, t;
int change [11] [11], heat [11] [11];
int seq [(1 << 10) + 1] [7];
int tube [11];

int dp (int S, int K)
{
    if (seq [S] [K] <inf || 0 == S) // After finding a solution, or returning without a solution
        return seq [S] [K];

    if (seq [S] [K] == inf + 1) // No solution
        return seq [S] [K];

    for (int i = 1; i <= ((1 << k)-1); i ++)
    {
        int tmpa, tmpb;
        if ((i & S) == i)
        {
            for (int x = 1; x <= m; x ++)
            {
                for (int y = 1; y <= m; y ++)
                {
                    if (change [x] [y]! = K)
                        continue;
                    tmpa = dp (S ^ i, x);
                    if (tmpa <inf)
                    {
                        tmpb = dp (i, y);
                        if (tmpb <inf)
                        {
                            seq [S] [K] = min (seq [S] [K], tmpa + tmpb + heat [x] [y]);
                        }
                    }
                }
            }
        }
    }
    if (seq [S] [K] == inf) // It has been searched, and it is determined that there is no solution. Remember to save the state, otherwise it will search again and time out
        seq [S] [K] ++; // + 1 on inf, which means the state is unreachable, and it has been searched
    return seq [S] [K];
}

int main ()
{
    ios :: sync_with_stdio (false);
    cin >> t;
    while (t--)
    {
        cin >> m;
        for (int i = 1; i <= m; i ++)
        {
            for (int j = 1; j <= m; j ++)
            {
                int c, h;
                cin >> c >> h;
                change [i] [j] = c;
                heat [i] [j] = h;
            }
        }
        cin >> k;
        for (int i = 1; i <= k; i ++)
            cin >> tube [i];
        for (int i = 0; i <(1 << 10) + 1; i ++)
        {
            for (int j = 0; j <7; j ++)
                seq [i] [j] = inf;
        }
        for (int i = 1, j = 1; i <= (1 << (k-1)); i = i << 1, j ++)
            seq [i] [tube [j]] = 0;

        for (int i = 1; i <= m; i ++)
        {
            dp ((1 << k) -1, i);
        }
        int ans = inf + 1;
        for (int i = 1; i <= m; i ++)
        {
            ans = min (ans, seq [(1 << k)-1] [i]);
        }
        cout << ans << endl;
        string s;
        cin >> s;
    }
    return 0;
}


Ideas:



The typical dynamic programming problem, which can be seen from the data volume in the topic, is suitable for solving the problem by state compression method. Because the number of States is small, you can also use a multidimensional array method.



Set Seq[s][k] to generate K for this type of drug in the case of state S, S is the value of the state-compressed binary each one holds the drug that participates in the reaction. For example, when s=0101, there are 1th and 3rd drugs participating in the reaction (from left to right number)



So seq[0101][2] represents the heat generated by the 1th and 3rd drug reactions that produce a second drug.
CHANGE[I][J] Represents a drug conversion table
HEAT[I][J] means releasing heat.
Tube[i] to show you the medicine



Because the state to transfer is not continuous, so to use the method of memory search, the search function is DP (S,K), the letter meaning ibid.



Let's say a wrong state transfer equation.



Seq[s][k]=min (SEQ[S][K],DP (s^i,j) +min (Heat[tube[j][x]],heat[tube[x][j]])


The above I represents the binary of the first few, that is, the binary form represents the first few drugs, x indicates that the binary of I is the first few drugs.



The above release takes into this data
3
1 0
3-10
3 3000
3-10
2 0
1-500
3 3000
1-500
3 0
4
1 2 2 3
The correct result is-240
But it is wrong to follow the above-mentioned results, as the mix of drugs cannot be considered in a previously mixed state with another "1" New drug. Because that's probably the way it's mixed.
SEQ[1001][1] + seq[0110][2] +heat[1][2]
Seq[1001][1] Represents the 1th and 4th hybrid builds of 1
SEQ[0110][2] Represents the 2nd and 3rd hybrid builds 2



So the correct state transfer equation should be



Seq[s][k]=min (SEQ[S][K],DP (s^i,x) +DP (i,y) +heat[x][y])
x and y take the value range m
I value from each bit above to enumerate all binary states less than S

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.