// Use the cross linked list to store c = a * B for sparse matrix multiplication. The z1240 times out.
# Include <stdio. h>
# Include <stdlib. h>
Typedef struct mnode
{
Int row, Col, V;
Struct mnode * down, * right;
} Mnode;
Typedef struct
{
Int Mu, nu, Tu;
Mnode * rlink [10001], * clink [10001];
} Crosslink;
Crosslink * creatcrosslink2 (crosslink * H, int I, Int J, int v) // create a sparse matrix for cross-linked list storage Step 2
{
Mnode * P, * q;
P = (mnode *) malloc (sizeof (mnode ));
P-> ROW = I;
P-> Col = J;
P-> V = V;
Q = H-> rlink [I];
If (q = NULL | Q-> Col> J) // insert * P as the first node.
{
P-> right = Q;
H-> rlink [I] = P;
}
Else // find the position of P in row I
{
While (Q-> Right & (Q-> right-> col) <j)
Q = Q-> right;
P-> right = Q-> right;
Q-> right = P; // inserts P on the row chain.
}
// Insert P to the column linked list and sort it by row number
Q = H-> clink [J];
If (q = NULL | Q-> row> I) // insert * P as the first node.
{
P-> down = Q;
H-> clink [J] = P;
}
Else
{
While (Q-> down & (Q-> down-> row) <I)
Q = Q-> down;
P-> down = Q-> down;
Q-> down = P; // inserts P on the column chain.
}
Return h;
}
Crosslink * creatcrosslink1 (crosslink * h) // create the sparse matrix of the cross linked list storage Step 2
{
Int I, J, K, V;
H = (crosslink *) malloc (sizeof (crosslink ));
Scanf ("% d", & H-> Mu, & H-> Nu, & H-> Tu ); // number of rows, columns, and non-zero elements in the sparse matrix
For (k = 1; k <= H-> mu; k ++) // Initialization
H-> rlink [k] = NULL;
For (k = 1; k <= H-> Nu; k ++)
H-> clink [k] = NULL;
For (k = 1; k <= H-> Tu; k ++)
{
Scanf ("% d", & I, & J, & V); // size of the input rows, columns, and non-zero elements
Creatcrosslink2 (H, I, J, V );
}
Return h;
}
Int main ()
{// Freopen ("1.txt"," r ", stdin );
Crosslink * a, * B, * C;
Mnode * AQ, * Bq, * q;
Int I, K, temp;
C = (crosslink *) malloc (sizeof (crosslink ));
A = (crosslink *) malloc (sizeof (crosslink ));
B = (crosslink *) malloc (sizeof (crosslink ));
A = creatcrosslink1 ();
B = creatcrosslink1 (B );
C-> mu = A-> mu;
C-> Nu = B-> Nu;
If (a-> Nu! = B-> mu)
Return 0;
Else if (a-> tu * B-> Tu = 0)
{
Printf ("% d 0 \ n", C-> Mu, C-> Nu );
Return 0;
}
Else
{
For (k = 1; k <= C-> mu; k ++)
C-> rlink [k] = NULL;
For (k = 1; k <= C-> Nu; k ++)
C-> clink [k] = NULL;
C-> Tu = 0;
For (I = 1; I <= (a-> mu); I ++)
{
Q = A-> rlink [I]; // the first element of row I in the Matrix
If (q)
{
For (Int J = 1; j <= B-> Nu; j ++)
{
Temp = 0;
AQ = Q;
BQ = B-> clink [J]; // the first element of column J in the B Matrix
While (AQ & Bq)
{
// Printf ("..... I = % d J = % d v = % d \ n ", AQ-> row, AQ-> Col, AQ-> V );
// Printf ("B1 ..... I = % d J = % d v = % d \ n ", BQ-> row, BQ-> Col, BQ-> V );
If (AQ-> Col = BQ-> row) // The column where the AQ of the matrix element is located is the same as the row where the Bq of the B matrix element is located.
{
Temp = temp + (AQ-> V) * (BQ-> V );
AQ = AQ-> right; // obtain the next element of the row where the AQ element of matrix A is located.
BQ = BQ-> down; // obtain the next element of the column where the BQ element of the B matrix is located.
}
Else if (AQ-> Col <BQ-> row) // obtain the next element of the row where the AQ element of matrix A is located.
AQ = AQ-> right;
Else // obtain the next element of the column in which the BQ element belongs.
BQ = BQ-> down;
} // While
If (temp! = 0)
{
(C-> Tu) ++;
Creatcrosslink2 (C, I, j, temp );
// Printf ("I = % d J = % d temp = % d \ n", I, j, temp );
}
} // For J
} // If
} //
If (c-> Tu) = 0)
Printf ("% d 0 \ n", C-> Mu, C-> Nu );
Else
{
Printf ("% d \ n", C-> Mu, C-> Nu, C-> Tu );
For (I = 1; I <= C-> mu; I ++)
{
Q = C-> rlink [I];
While (q)
{
Printf ("% d \ n", Q-> row, Q-> Col, Q-> V );
Q = Q-> right;
}
}
}
}
Return 0;
}