diff options
Diffstat (limited to 'problem.h')
-rw-r--r-- | problem.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/problem.h b/problem.h new file mode 100644 index 0000000..678ab59 --- /dev/null +++ b/problem.h @@ -0,0 +1,126 @@ +#pragma once + +#include <iostream> +#include <vector> +#include <utility> + +using namespace std; + +template <typename T> +class Matrix{ + vector<vector<T>> mat; //list of rows + int W=0,H=0; + +public: + template <typename U> + class RowAccess{ + vector<T> &row; + public: + RowAccess(vector<T> &row):row(row){} + T& operator[](int x){return row[x];} + const T& operator[](int x) const {return row[x];} + T& front(){return row.front();} + const T& front() const {return row.front();} + T& back(){return row.back();} + const T& back() const {return row.back();} + }; + + template <typename U> + class ConstRowAccess{ + const vector<T> &row; + public: + ConstRowAccess(const vector<T> &row):row(row){} + const T& operator[](int x) const {return row[x];} + const T& front() const {return row.front();} + const T& back() const {return row.back();} + }; + + Matrix() = default; + + Matrix(int W,int H) + :W(W),H(H){ + mat.resize(H); + for(auto &row : mat){ + row.resize(W); + } + } + + void addrow(){ + mat.emplace_back(W,T()); + H++; + } + + void addcolumn(){ + for(auto &row : mat){ + row.emplace_back(); + } + W++; + } + + void resize(int w,int h){ + if(h<H){ + H=h; + mat.resize(H); + } + if(w!=W){ + W=w; + for(auto &row : mat){ + row.resize(w); + } + } + if(h>H){ + mat.resize(h); + for(int i=H;i<h;i++){ + mat[i].resize(W); + } + H=h; + } + } + + void clear(){ + mat.clear(); + } + + RowAccess<T> operator[](int y){ + return RowAccess<T>(mat[y]); + } + + ConstRowAccess<T> operator[](int y) const { + return ConstRowAccess<T>(mat[y]); + } + + int width() const {return W;} + + int height() const {return H;} +}; + +class Problem{ + enum VarType{ + VT_NORMAL=0, + VT_SLACK, + VT_ART, + }; + vector<VarType> vartype; + vector<double> zfunc; + double zvalue=0; + Matrix<double> restr; + vector<double> bvec; + vector<int> basis; + + void removefrombasis(int varidx); + void solve_noart(); + +public: + Problem(const Problem&) = default; + Problem(istream &in); + + void solve(); + double solutionValue() const; + vector<double> solutionVars() const; + + friend istream& operator>>(istream &in,Problem &prob); + friend ostream& operator<<(ostream &os,const Problem &prob); +}; + +istream& operator>>(istream &in,Problem &prob); +ostream& operator<<(ostream &os,const Problem &prob); |