稀疏矩阵的乘法

计算两个稀疏矩阵的乘法

首先输入第一个矩阵的行数和列数,再输入该矩阵的三元组形式,以0 0 0结束。

然后输入第二个矩阵的行数和列数,再输入该矩阵的三元组形式,以0 0 0结束。

输出相加后的矩阵三元组。

3 3

1 1 1

2 2 2

2 3 4

3 1 -4

0 0 0

3 3

1 3 -2

2 3 -5

3 1 8

3 2 -6

0 0 0

1 3 -2

2 1 32

2 2 -24

2 3 -10

3 3 8

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
using namespace std;
class node
{
public:
    int x,y;
    double num;
    node*prev;
    node*next;
    node(int xin,int yin,double numin,node*previn,node*nextin)
    {
        x=xin;
        y=yin;
        num=numin;
        prev=previn;
        next=nextin;
        if(next)next->prev=this;
        if(prev)prev->next=this;
    }
};
class matrix
{
public:
    node*head;//链表头结点
    node*tail;//链表尾节点
    int hang;//矩阵行数
    int lie;//矩阵列数
    node**locate;//locate[i]第i行首个元素的地址
    matrix(int h,int l)
    {
        head=NULL;
        tail=NULL;
        hang=h;
        lie=l;
        locate=(node**)malloc((hang+1)*sizeof(node*));
        for(int i=0;i<=hang;i++)locate[i]=NULL;
    }
    void insert(int x,int y,double num)
    {
        if(num==0)return;
        if(!head)head=tail=locate[x]=new node(x,y,num,NULL,NULL);
        else if(locate[x]!=NULL)
        {
            node*p=locate[x];
            if(p->y>y)locate[x]=new node(x,y,num,p->prev,p);
            else if(p->y==y)p->num+=num;
            else
            {
                while(p->next&&p->next->x==x&&p->next->y<y)p=p->next;
                if(p->next==NULL)tail=new node(x,y,num,p,NULL);
                else if(p->next->y==y)p->next->num+=num;
                else new node(x,y,num,p,p->next);
            }
        }
        else
        {
            int i;
            for(i=x;i<=hang;i++)if(locate[i])break;
            if(i==hang+1)tail=locate[x]=new node(x,y,num,tail,NULL);
            else locate[x]=new node(x,y,num,locate[i]->prev,locate[i]);
        }
        while(head->prev!=NULL)head=head->prev;
    }
    matrix* operator *(const matrix &another)
    {
        matrix* ret=new matrix(this->hang,another.lie);
        node*p;
        while(head&&head->prev)head=head->prev;
        for(p=this->head;p;p=p->next)
        {
            node*q=another.locate[p->y];
            while(q&&q->x==p->y)
            {
                ret->insert(p->x,q->y,p->num*q->num);
                q=q->next;
            }
        }
        return ret;
    }
    void print()
    {
        node*q=head;
        while(q&&q->prev)q=q->prev;
        while(q)
        {
            if(q->num!=0)printf("%d %d %.f\n",q->x,q->y,q->num);//cout<<q->x<<" "<<q->y<<" "<<q->num<<endl;
            q=q->next;
        }
    }
    ~matrix()
    {
        free(locate);
        while(tail!=head)
        {
            tail=tail->prev;
            delete tail->next;
        }
        delete tail;
    }
};
int main()
{
    //freopen("data.in","r",stdin);
    int hang,lie;
    cin>>hang>>lie;
    int x,y;
    double num;
    matrix a(hang,lie);
    while(1)
    {
        cin>>x>>y>>num;
        if(x==0&&y==0&&num==0)break;
        a.insert(x,y,num);
    }
    cin>>hang>>lie;
    matrix b(hang,lie);
    while(1)
    {
        cin>>x>>y>>num;
        if(x==0&&y==0&&num==0)break;
        b.insert(x,y,num);
    }
    (a*b)->print();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值