D - Matrix Chain Multiplication

本文介绍了一种使用栈和映射来高效计算矩阵乘法的方法,通过解析特定的输入格式,可以快速评估各种矩阵运算组合所需的乘法次数。适用于解决算法竞赛中的矩阵运算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))
Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125

在输入中第一个数n表示矩阵的个数;接下来n行输入系列矩阵的名字,行和列;然后输入指令,就是括号和一些表矩阵的字母;要你用矩阵的运算法则求每个指令用了多少乘法;如果不能乘法就输出error;

思路;输入矩阵信息时,把矩阵名也就是字母作为map的key,矩阵的行和列作为map的value,然后只要调用矩阵名就能得到矩阵的行,列;这里map相当于一个数组;接下来用字符串接受指令,并开一个栈,当指令是左括号或字母时入,右括号时出三个元素,前两个是矩阵名,后一个是左括号;然后形成新的矩阵,并且用新的字母作为矩阵名入map 


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <map>
using namespace std;

typedef struct {char ch;int r,c;}matix;
matix s[40];
int main()
{
    char T[26];
    for(int i=0;i<26;i++)T[i]='a'+i;
    int n;map<char,pair<int,int> >s;
    //freopen("1.txt","r",stdin);
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            char c;int a,b;
            cin>>c>>a>>b;
            s.insert(make_pair(c,make_pair(a,b)));
        }
        s.insert(make_pair('ch',make_pair(0,0)));
        string str;
        while(cin>>str)
        {
            if(str.length()==1)cout<<"0\n";
            else
            {
                stack<char>fu;int co=0;int flag=0;
                for(int i=0;i<(int)str.length();i++)
                {
                    if(str[i]=='('||isalpha(str[i]))fu.push(str[i]);
                    else if(str[i]==')')
                    {
                        char te1=fu.top();fu.pop();char te2=fu.top();fu.pop();fu.pop();fu.push(T[i]);
                        if(s[te2].second==s[te1].first)
                        {
                            co+=s[te2].first*s[te2].second*s[te1].second;
                            s[T[i]].first=s[te2].first,s[T[i]].second=s[te1].second;
                        }
                        else {flag=1;break;}
                    }
                }
                if(flag)cout<<"error\n";
                else
                    cout<<co<<endl;
            }
        }
    }

}


import cvxpy as cp import numpy as np import pandas as pd data = pd.read_excel('附件1.xlsx') X = data.iloc[1:, 1:2] Y = data.iloc[1:, 2:3] W = data.iloc[2:, 3:4] # 收集点垃圾量(i=1..n) xi = X.values.flatten() # 转换为一维数组 (n,) yi = Y.values.flatten() # 转换为一维数组 (n,) wi = W.values.flatten() # 转换为一维数组 (n,) # 定义参数 n = 30 # 收集点数量(i=1..n,0为处理厂) K = 30 # 车辆总数 Q = 5 # 最大载重 # 计算距离矩阵dij(i,j=0..n) dij = np.zeros((n+1, n+1)) for i in range(n+1): for j in range(n+1): dij[i, j] = np.sqrt((xi[i] - xi[j])**2 + (yi[i] - yi[j])**2) x = cp.Variable((n+1, n+1, K), boolean=True) # x_ijv: 车辆v从i到j y = cp.Variable((n, K), boolean=True) # y_iv: 车辆v服务收集点i(i=1..n,索引0..n-1) z = cp.Variable(K, boolean=True) # z_v: 使用车辆v u = cp.Variable((n+1, K), nonneg=True) # u_iv: 车辆v访问i后的累计载重(i=0..n,u[0,v]=0) objective = cp.Minimize(cp.sum(cp.sum(cp.sum(dij @ x, axis=2), axis=1), axis=0)) # 约束条件 constraints = [] # 1. 每个收集点被 exactly 一辆车服务(i=1..n,对应y的索引0..n-1) for i in range(n): constraints.append(cp.sum(y[i, :]) == 1) # 2. 流量守恒(i=1..n,处理厂i=0的约束在6中) for i in range(1, n+1): # 收集点i(1..n,y索引i-1) for v in range(K): constraints.append(cp.sum(x[i, :, v]) == y[i-1, v]) # 离开i的次数等于服务i的次数 constraints.append(cp.sum(x[:, i, v]) == y[i-1, v]) # 进入i的次数等于服务i的次数 # 3. 载重约束 for v in range(K): constraints.append(cp.sum(wi * y[:, v]) <= Q * z[v]) # 4. MTZ约束(子回路消除) for v in range(K): constraints.append(u[0, v] == 0) # 处理厂出发时载重为0 for i in range(1, n+1): # 收集点i(1..n,wi[i-1]) constraints.append(u[i, v] >= wi[i-1]) # 载重下限 constraints.append(u[i, v] <= Q) # 载重上限 for j in range(1, n+1): if i != j: constraints.append(u[i, v] - u[j, v] + Q * x[i, j, v] <= Q - wi[j-1]) # 5. 车辆使用约束:服务收集点则使用车辆 for i in range(n): for v in range(K): constraints.append(y[i, v] <= z[v]) # 6. 出发返回约束(处理厂i=0,出发到收集点1..n,返回从收集点1..n到0) for v in range(K): constraints.append(cp.sum(x[0, 1:, v]) == z[v]) # 出发次数等于车辆使用 constraints.append(cp.sum(x[1:, 0, v]) == z[v]) # 返回次数等于车辆使用 # 建立问题并求解 problem = cp.Problem(objective, constraints) problem.solve() # 输出结果(示例,需根据实际需求处理) print("最优目标值:", problem.value) print("使用的车辆:", z.value)D:\python\python.exe D:\35433\Documents\数模练习\电工杯\问题1.py D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 1 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 2 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 3 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 4 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 5 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 6 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 7 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 8 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 9 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 10 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 11 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 12 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 13 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 14 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 15 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 16 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 17 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 18 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 19 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 20 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 21 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 22 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 23 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 24 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 25 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 26 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 27 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 28 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 29 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 30 times so far. D:\python\Lib\site-packages\cvxpy\reductions\solvers\solving_chain_utils.py:41: UserWarning: The problem has an expression with dimension greater than 2. Defaulting to the SCIPY backend for canonicalization.
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值