参考数据结构严蔚敏稀疏矩阵乘法
1 #include<iostream> 2 #include<vector> 3 #include<unistd.h> 4 #include<fstream> 5 6 using namespace std; 7 class Tuple{ 8 public: 9 int row,col,val; 10 Tuple():row(0),col(0),val(0){} 11 Tuple(int ar,int ac,int av):row(ar),col(ac),val(av){} 12 }; 13 class Spamat{ 14 public: 15 //in order to eliminate syntax ambiguity, cannot use parentheses for in-class initialze, only brackets 16 //vector<Tuple> tup{100}; ok 17 //vector<Tuple> tup(100); error 18 vector<Tuple> tup; 19 int rownum,colnum,cnt; 20 Spamat():rownum(0),colnum(0),cnt(0){} 21 Spamat(int rn,int cn,int ct):rownum(rn),colnum(cn),cnt(ct){} 22 }; 23 class Solution{ 24 public: 25 Spamat mat1,mat2,mat3; 26 fstream datasrc{"data.txt",ios::in}; 27 void input(){ 28 int row,col,val; 29 // cout << "input the information of the first matrix with format: "; 30 // cout << "rownum colnum count ... "; 31 datasrc >> mat1.rownum >> mat1.colnum >> mat1.cnt; 32 for(int i=0;i<mat1.cnt;++i){ 33 datasrc >> row >> col >> val; 34 mat1.tup.push_back(Tuple(row,col,val)); 35 } 36 // cout << "input the information of the second matrix with format: "; 37 // cout << "rownum colnum count ... "; 38 datasrc >> mat2.rownum >> mat2.colnum >> mat2.cnt; 39 for(int i=0;i<mat2.cnt;++i){ 40 datasrc >> row >> col >> val; 41 mat2.tup.push_back(Tuple(row,col,val)); 42 } 43 cout << "input ok" << endl; 44 //pause(); 45 } 46 void output(Spamat &mat){ 47 cout << "cnt : " << mat.cnt << endl; 48 for(auto t : mat.tup){ 49 cout << t.row << " " << t.col << " " << t.val << endl; 50 } 51 cout << "out ok" << endl; 52 } 53 void solve(){ 54 input(); 55 output(mat1); 56 output(mat2); 57 vector<int> rowcnt(100,0); 58 vector<int> rowpos(100,0); 59 vector<int> temp(100,0); 60 for(int i=0;i<mat2.cnt;++i){ 61 rowcnt[mat2.tup[i].row]++; 62 } 63 rowpos[1] = 0; 64 for(int i=2;i<=mat2.rownum;++i){ 65 rowpos[i] = rowpos[i-1] + rowcnt[i-1]; 66 } 67 for(int i=1;i<=mat1.rownum;++i){ 68 //temp.clear(); 69 std::fill(temp.begin(),temp.end(),0); 70 for(int j=0;j<mat1.cnt;++j){ 71 if(mat1.tup[j].row > i)break; 72 else if(mat1.tup[j].row == i){ 73 //cout << "in mat1: " << i << " " << j << endl; 74 int tcol = mat1.tup[j].col; 75 //cout << "~" << " " << rowpos[tcol] << " ~ " << rowcnt[tcol]+rowpos[tcol] << endl; 76 for(int k=rowpos[tcol];k<rowpos[tcol]+rowcnt[tcol];++k){ 77 78 temp[mat2.tup[k].col] += mat1.tup[j].val * mat2.tup[k].val; 79 //cout << "temp ; " <<mat2.tup[k].col<<" " << temp[mat2.tup[k].col] << endl; 80 } 81 82 } 83 } 84 for(int j=1;j<=mat2.colnum;++j){ 85 if(temp[j] != 0){ 86 mat3.tup.push_back(Tuple(i,j,temp[j])); 87 } 88 } 89 } 90 mat3.cnt = mat3.tup.size(); 91 output(mat3); 92 } 93 }; 94 int main() 95 { 96 Solution sol; 97 sol.solve(); 98 return 0; 99 }
稀疏矩阵加法:
1 #include <iostream> 2 #include<vector> 3 4 using namespace std; 5 class Tuple{ 6 public: 7 int row,col,val; 8 Tuple():row(0),col(0),val(0){} 9 Tuple(int tr,int tc,int tv):row(tr),col(tc),val(tv){} 10 }; 11 vector<Tuple> mat1,mat2,mat3; 12 int rownum,colnum,len1,len2; 13 int cmpmat(Tuple &t1,Tuple &t2); 14 int main() 15 { 16 int tr,tc,tv; 17 cin >> rownum >> colnum >> len1 >> len2; 18 mat1.reserve(len1); 19 mat2.reserve(len2); 20 mat3.reserve(len1+len2); 21 for(int i=0;i<len1;++i){ 22 cin >> tr >> tc >> tv; 23 mat1.push_back(Tuple(tr,tc,tv)); 24 } 25 for(int i=0;i<len2;++i){ 26 cin >> tr >> tc >> tv; 27 mat2.push_back(Tuple(tr,tc,tv)); 28 } 29 vector<Tuple>::iterator iter1 = mat1.begin(),iter2 = mat2.begin(); 30 while(iter1!=mat1.end()&&iter2!=mat2.end()){ 31 if(cmpmat(*iter1,*iter2)==0){ 32 tv = (*iter1).val+(*iter2).val; 33 if(tv){ 34 mat3.push_back(Tuple(iter1->row,iter1->col,tv)); 35 } 36 ++iter1; 37 ++iter2; 38 continue; 39 } 40 if(cmpmat(*iter1,*iter2)<0){ 41 mat3.push_back(Tuple(iter1->row,iter1->col,iter1->val)); 42 ++iter1; 43 } 44 else{ 45 mat3.push_back(Tuple(iter2->row,iter2->col,iter2->val)); 46 ++iter2; 47 } 48 } 49 if(iter1!=mat1.end()){ 50 for(;iter1!=mat1.end();++iter1){ 51 mat3.push_back(Tuple(iter1->row,iter1->col,iter1->val)); 52 } 53 } 54 if(iter2!=mat2.end()){ 55 for(;iter2!=mat2.end();++iter2){ 56 mat3.push_back(Tuple(iter2->row,iter2->col,iter2->val)); 57 } 58 } 59 for(auto tp : mat3){ 60 cout << tp.row << " " << tp.col << " " << tp.val << endl; 61 } 62 return 0; 63 } 64 int cmpmat(Tuple &t1,Tuple &t2) 65 { 66 if(t1.row==t2.row&&t1.col==t2.col){ 67 return 0; 68 } 69 if((t1.row<t2.row)||(t1.row==t2.row)&&(t1.col<t2.col)){ 70 return -1; 71 } 72 return 1; 73 }