前言
集训时用到了,干脆也过一下模板,记一下老师说的相关内容。
定义
行乘列,无需多言。
这里贴一个帅板子
template <LL N, LL M, class T = LL>
struct Mat {
T a[N][M];
Mat() { // 构造函数随便改
for (LL i = 0; i<N; i++)
for (LL j = 0; j<M; j++)
a[i][j] = -INF;
}
void init() { // 功能函数随便改
for (LL i = 0; i<N; i++)
for (LL j = 0; j<M; j++)
a[i][j] = -INF;
for (LL i = 0; i<N; i++)
a[i][i] = 0;
}
// 可以加点其他功能
T* operator[](LL i) { // 很方便
return a[i];
}
friend bool operator!=(const Mat &x, const Mat &y) { // 判等
for (LL i = 0; i<N; i++)
for (LL j = 0; j<M; j++)
if (x[i][j] != y[i][j])
return true;
return false;
}
void print(string s) { // 条
string k = "prt " + s + " mat:";
cerr << k << endl;
for (LL i = 0; i<N; i++)
for (LL j = 0; j<M; j++)
cerr << a[i][j] << " \n"[j==M-1];
}
};
template<LL N, LL M, LL R, class T = LL>
Mat<N, R, T> operator*(Mat<N, M, T> x, Mat<M, R, T> y) { // 核心出装,这里是个类乘
Mat<N, R, T> res;
for (LL i = 0; i<N; i++)
for (LL j = 0; j<R; j++)
for (LL k = 0; k<M; k++)
res[i][j] = max(res[i][j], x[i][k]+y[k][j]);
return res;
}
template<LL N, LL M, class T = LL>
Mat<N, M> operator+(Mat<N, M> x, Mat<N, M> y) { // 这里是个类加
for (LL i = 0; i<N; i++)
for (LL j = 0; j<N; j++)
x[i][j] = max(x[i][j], y[i][j]);
return x;
}广义矩阵乘法
简单来说,就是用具有结合律的运算替代矩阵乘法中的加与乘,仍然满足矩阵乘法的性质。
一些题目
P6190
P8820
P5024