首页 > 其他分享 >1179 Chemical Equation(搜索 + 回溯)

1179 Chemical Equation(搜索 + 回溯)

时间:2024-11-28 20:55:14浏览次数:9  
标签:temp int auto Equation cin 1179 Chemical products possess

 先把各产物对应的公式按题面要求的从小到大进行排序(丢set里让他自己排序就行),搜索条件有两个:

  1.每个原料最多使用一次

  2.每个产物都要被生成

排序后,搜索得到的第一个解就是最优解。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n, m, k;
 4 vector<string> products(25); //按序记录产物
 5 map<string, set<vector<string>>> equation; //记录所有公式
 6 map<string, bool> possess; //是否拥有该原料
 7 
 8 void output(map<string, set<string>> result) {
 9     for (int i = 0; i < m; ++ i) {
10         auto j = result.find(products[i]);
11         if (j == result.end()) continue;
12         int index = 0;
13         for (auto l : j -> second) {
14             cout << l;
15             index ++;
16             if (index != j -> second.size()) {
17                 cout << " + ";
18             }
19         }
20         cout << " -> " << products[i] << endl;
21     }
22 }
23 void back_travel(int start, map<string, set<string>> solution) {
24     if (solution.size() == m) {
25         output(solution);
26         exit(0); //不用继续回溯,按顺序找到的第一个就是最优解
27         return;
28     }
29     auto iter = equation.find(products[start]);
30     for (auto i : iter -> second) {
31         bool is_enough = true;
32         for (auto j : i) {
33             if (!possess[j]) {
34                 is_enough = false;
35                 break;
36             }
37         }
38         if (is_enough) {
39             map<string, set<string>> temp = solution;
40             for (auto j : i) {
41                 possess[j] = false;
42                 temp[products[start]].insert(j);
43             }
44             back_travel(start + 1, temp);
45             for (auto j : i) { 
46                 possess[j] = true;
47             }
48         }
49     }
50 }
51 int main() {
52     cin >> n;
53     for (int i = 0; i < n; ++ i) {
54         string reactant;
55         cin >> reactant;
56         possess[reactant] = true;
57     }
58     cin >> m;
59     for (int i = 0; i < m; ++ i) {
60         cin >> products[i];
61         vector<string> temp_s;
62         temp_s.push_back(products[i]);
63         equation[products[i]].insert(temp_s); //将x->x添加到公式中
64     }
65     cin >> k;
66     for (int i = 0; i < k; ++ i) {
67         vector<string> temp_s;
68         string temp;
69         while(cin >> temp) {
70             if (temp == "->") {
71                 break;
72             } else if (temp == "+") {
73                 continue;
74             }
75             temp_s.push_back(temp);
76         }
77         cin >> temp;
78         equation[temp].insert(temp_s);
79     }
80     back_travel(0, {});
81 }

 

标签:temp,int,auto,Equation,cin,1179,Chemical,products,possess
From: https://www.cnblogs.com/coderhrz/p/18575142

相关文章

  • [题解]CF1775E The Human Equation
    来个另类解。思路手玩一下样例,发现减法只会用在正数上,加法只会用在负数上,大概是因为如何在负数上用了减法或在正数上用了加法,都需要额外的次数去消掉。然后注意到在两个正数中间包这的所有负数可以直接缩成一个数,两个负数中间包着的所有正数也可以直接缩成一个数。那么现在的序......
  • CF1775E The Human Equation
    CF1775ETheHumanEquation题目大意:给定\(n\)个数\(a_1...a_n\),随后你可以进行若干次操作,每次操作步骤如下:选出\(a\)中一个子序列(可以不连续)。把子序列中的奇数项减一,偶数项加一;或者奇数项加一,偶数项减一。求把\(n\)个数全部变成\(0\)的最少操作次数。思路:......
  • CBDD-Chemical Biology & Drug Design
    文章目录一、征稿简介二、重要信息三、服务简述四、投稿须知五、联系咨询一、征稿简介二、重要信息期刊官网:https://ais.cn/u/3eEJNv三、服务简述本次征文主题包括但不限于:虚拟筛选全新药物设计药物再利用毒性预测临床试验优化性质优化关键词:人工智能;自然......
  • Latex 两版排版下的长公式换行(equation & split)
    举例:二元高斯分布的密度函数(\(X\),\(Y\)不独立)\(f_{X,Y}\left(x,y\right)=\frac{1}{2\pi\sigma_{x}\sigma_{y}\sqrt{1-\rho^2}}\exp\left(-\frac{1}{2(1-\rho)^2}\left[\frac{(x-\mu_{x})^2}{\sigma_{x}^2}-2\rho\frac{(x-\mu_{x})(t-\mu_{y})}{\sigma_{x}\si......
  • SciTech-Mathmatics-Physics-Particle+Movement-Election-The Maxwell Equations-Wave
    TheMaxwellEquations:电、磁、光StaticElectricFieldStaticMagneticFieldChangingElectricFieldChangingMagneticField......
  • ECOS3010 mathematical equations
    ECOS3010:Assignment1(Total:20marks)Due11:59pm,FridayAug30,2024Homeworkmustbeturnedinonthedayitisdue.Worknotsubmittedonorbeforetheduedateissubjecttoapenaltyof5%percalendardaylate.Ifworkissubmittedmorethan......
  • SciTech-Mathmatics-Physics-Particle Physics-Election-The Maxwell Equations-Wave-
    TheMaxwellEquations:Election,Substances,Particle'sBrownMovementsAZD(AbsoluteZeroDegree):EachkindofparticlehasitswavewhenaboveAZD.TheMaxwellEquations:\(\large\begin{array}{llll}\\(\i\)&\bm{\nabla}\cd......
  • ECOS3010 mathematical equations
    ECOS3010:Assignment1(Total:20marks)Due11:59pm,FridayAug30,20241.Homeworkmustbeturnedinonthedayitisdue.Worknotsubmittedonorbeforetheduedateissubjecttoapenaltyof5%percalendardaylate.Ifworkissubmittedmorethan......
  • OFtutorial10_transportEquation解析
    组成OFtutorial10.C源码头文件#include"fvCFD.H"主函数intmain(intargc,char*argv[]){头文件 //Setupthecase,parsecommandlineoptionsandcreatethegrid#include"setRootCase.H"#include"createTime.H"#inclu......
  • 微分方程(Blanchard Differential Equations 4th)中文版Section3.3
    具有实特征值的线性系统的相图在前面的部分,我们看到直线解在求解某些线性微分方程系统的通解中起着主导作用。为了求解这样的系统,我们首先使用代数方法计算系数矩阵的特征值和特征向量。当我们找到一个实特征值和一个相关的特征向量时,就可以写出对应的直线解。此外,在特定情......