Subsy matrix is ​​added

7-1 Sparse Matrix plus method (20 points)
 

给定两个矩阵a andB, ask for and matrixC=A+b.b.

Enter the format:

The first line contains two numbersRoW andCoL, represent the number of rows and columns of the matrix, respectively,a andB The dimension is consistent.

Only one number in the second lineN1Soups that you want to enter nextNumber of non-zero elements in the AA.

Next isN1Row, every line isi j A[i,j] This form is representedAI linej elementsA[i,j] In order to be consistent with most programming languages, they are from zero, that is, the effective range of the subscript is[0,Row1]×[0,Col1]].

N1After the line, it is a numberN2, represent matrixThe number of non-zero elements in B, after thisN2Row descriptionB non-zero element, they are described with previous descriptionThe form of non-zero elements in 3.

The input of matrix elements follows the leading position. All inputs here are availableintThe type is correct, you can assume that the input is legal.

Output format:

The first line output and matrixC=A+B中的绝对值大于0.1的元素个数N3N3​​Row, output the non-zero element in the row, in turn, the subscript and the corresponding element.

Enter the sample:

2 2
1
1 1 1
1
0 0 1
 
There is no vacant

Output Sample:

2
0 0 1
1 1 1
 
There is no vacant
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x,y,z;
};
static bool cmp(node & a,node & b)
{
    if(a.x<b.x)return true;
    if(a.x==b.x&&a.y<b.y)return true;
    return false;
}
int main()
{
    int n,m;
    cin>>n>>m;
    vector<vector<int>> a,b;
    int k1;
    cin>>k1;
    for(int i=0;i<k1;i++){
        int x,y,z;
        cin>>x>>y>>z;
        vector<int> t;
        t.push_back(x);
        t.push_back(y);
        t.push_back(z);
        a.push_back(t);
    }
    int k2;
    cin>>k2;
    for(int i=0;i<k2;i++){
        int x,y,z;
        cin>>x>>y>>z;
        vector<int> t;
        t.push_back(x);
        t.push_back(y);
        t.push_back(z);
        
        b.push_back(t);
    }
    vector<node> ans;
    for(int i=0;i<k1;i++)
    {
       for(int j=0;j<k2;j++)
       {
           if(a[i][0]==b[j][0]&&a[i][1]==b[j][1])a[i][2]+=b[j][2];
       }
        node t;
        t.x=a[i][0];
        t.y=a[i][1];
        t.z=a[i][2];
        if(abs(t.z)>0.1)
        ans.push_back(t) ;
    }
    
    for(int j=0;j<k2;j++)
    {
        int flag=0;
        for(int i=0;i<k1;i++)
        {
             if(a[i][0]==b[j][0]&&a[i][1]==b[j][1])
             {
                 flag=1;
                 break;
             }
        }
        node t;
        t.x=b[j][0];
        t.y=b[j][1];
        t.z=b[j][2];
        if(flag==0)ans.push_back(t);
    }
    sort(ans.begin(),ans.end(),cmp);
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++)
    {
        cout<<ans[i].x<<' '<<ans[i].y<<' '<<ans[i].z;
        if(i<ans.size()-1)cout<<endl;
    }
    return 0;
}