HDU 4920 sparse matrix multiplication, hdu4920 Matrix Multiplication

Source: Internet
Author: User

HDU 4920 sparse matrix multiplication, hdu4920 Matrix Multiplication

Violent. Don't think too much.

T ^ T g is too bad


# Include <cstdio >#include <cstring> # include <algorithm> using namespace std; inline void rd (int & ret) {char c; do {c = getchar ();} while (c <'0' | c> '9'); ret = c-'0'; while (c = getchar ()> = '0' & c <= '9') ret = ret * 10 + (c-'0');} inline void ot (int) // output ins {if (a> 9) ot (a/10); putchar (a % 10 + '0');} const int MAX_N = 807; int n; int a [MAX_N] [MAX_N], B [MAX_N] [MAX_N]; int c [MAX_N] [MAX_N]; int main () {while (1 = scanf ("% d", & n) {for (int I = 0; I <n; ++ I) {for (int j = 0; j <n; ++ j) {int x; rd (x); a [I] [j] = x % 3 ;}} for (int I = 0; I <n; ++ I) {for (int j = 0; j <n; ++ j) {int x; rd (x ); B [I] [j] = x % 3 ;}} memset (c, 0, sizeof (c); for (int I = 0; I <n; ++ I) {for (int k = 0; k <n; ++ k) {if (a [I] [k] = 0) continue; for (int j = 0; j <n; ++ j) {c [I] [j] + = a [I] [k] * B [k] [j] ;}} for (int I = 0; I <n; ++ I) {for (int j = 0; j <n; ++ j) {if (j = 0) ot (c [I] [j] % 3); else {putchar (''); ot (c [I] [j] % 3 );}} puts ("") ;}} return 0 ;}



There is a program for the multiplication of sparse matrices, but I cannot explain it.

In the previous loop, the triple table storage will be restored to the normal operation by pressing the mark according to the matrix multiplication rule.
The final loop is to put the multiplication result into a new triple table. In fact, the program annotations are clear.

The problem of Sparse Matrix Multiplication

What about your complete program? This program code is messy, and the style is not good :-)

Int ctemp [N. nu + 1]; // can this be defined here ????????????
This is wrong. Dynamic Allocation is required.
Int * ctemp = new int [N. nu + 1]; delete [] ctemp after use; release memory

For (arow = 1; arow <= M. mu; ++ arow)
{Int I;
For (I = 1; I <= N. nu; I ++) ctemp [I] = 0; // What does this mean ???????????
In this case, array initialization appears to be a common case. Before using an array, initialize it to 0. Otherwise, it may be a large negative number.
Q. rpos [arow] = Q. tu + 1; // you can also find ???????
Here should be the multiplication operation part. You should first carefully understand the multiplication operation process and try to write one independently, then compare with other people's programs to make it easier to understand ^_^
I hope my answers will help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.