Chemical Reaction - UVa 10604 dp

                                        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

-650



题意:混合任意两瓶药水,会产生一瓶新的药水,然后会放出或吸收一定的能量,问最后合并到只剩一瓶时最少放出多少能量。

思路:看了下其他人的代码,基本上都是六维的dp……还好我有独立思考的习惯……于是乎……这道题被我做成了11进制的状压dp  %>_<%,dp[S]表示在S状态下最少放出的药水,vector <int> vc[i]表示在剩i瓶药水时有哪些情况。注意题目有坑,就是a类型和b类型混合,与b类型和a类型混合可能不一样(真坑,后来队友就给我讲浓硫酸和水的故事……然后就感觉自己二了)。时间挺快,0.029秒,好不容易又有排名靠前的题了。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int trans[15][15][2],dp[20000100],vis[20000100],pow11[15],T,t;
vector <int> vc[15];
char s[110];
void solve(int S,int m1,int m2,int num)
{ int type,eng,S2;
  type=trans[m1][m2][0];
  eng=trans[m1][m2][1];
  S2=S-pow11[m1]-pow11[m2]+pow11[type];
  if(vis[S2]==t)
   dp[S2]=min(dp[S2],dp[S]+eng);
  else
  { dp[S2]=dp[S]+eng;
    vis[S2]=t;
    vc[num-1].push_back(S2);
  }
}
int main()
{ int n,m,i,j,k,m1,m2,ans,S,len;
  pow11[1]=1;
  for(i=2;i<=7;i++)
   pow11[i]=pow11[i-1]*11;
  scanf("%d",&T);
  for(t=1;t<=T;t++)
  { scanf("%d",&m);
    for(i=1;i<=m;i++)
     for(j=1;j<=m;j++)
      scanf("%d%d",&trans[i][j][0],&trans[i][j][1]);
    scanf("%d",&n);
    for(i=0;i<=n;i++)
     vc[i].clear();
    S=0;
    for(i=1;i<=n;i++)
    { scanf("%d",&k);
      S+=pow11[k];
    }
    scanf("%s",s);
    dp[S]=0;
    vc[n].push_back(S);
    for(i=n;i>=2;i--)
    { len=vc[i].size();
      for(j=0;j<len;j++)
      { S=vc[i][j];
        for(m1=1;m1<=m;m1++)
        { if((S%pow11[m1+1])/pow11[m1]>=2)
           solve(S,m1,m1,i);
          for(m2=m1+1;m2<=m;m2++)
           if((S%pow11[m1+1])/pow11[m1]>0 && (S%pow11[m2+1])/pow11[m2]>0)
           { solve(S,m1,m2,i);
             solve(S,m2,m1,i);
           }
        }
      }
    }
    ans=1000000000;
    len=vc[1].size();
    for(i=0;i<vc[1].size();i++)
     ans=min(ans,dp[vc[1][i]]);
    printf("%d\n",ans);
  }
}



内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值