[Solved] zoj 3497 mistwald matrix fast power

Source: Internet
Author: User

After several months, I re-performed the task and finally got the AC!

The question seems to be graph theory, but the fact is matrix multiplication. According to the discrete mathematics book, bij indicates the number of paths from AI to AJ with a length of K in the new matrix B obtained by K power of the adjacent matrix.

The above method calculates the number of any two-point paths, and this topic requires that the end point cannot have an outbound edge. Based on the sauce purple, it is OK to set all the end points to 0, check whether K steps can reach the end point.

# Include <map> # include <set> # include <list> # include <queue> # include <deque> # include <stack> # include <string> # include <time. h> # include <cstdio> # include <math. h> # include <iomanip> # include <cstdlib> # include <limits. h> # include <string. h> # include <iostream> # include <fstream> # include <algorithm> using namespace STD; # define ll long # define min int_min # define Max int_max # define PI ACOs (-1.0) # define fre Freopen ("input.txt", "r", stdin) # define FF freopen ("output.txt", "W", stdout) int n, m, T; typedef struct node {int mat [30] [30];} MAT; MAT unit, Init; void Init () {char C; scanf ("% d ", & N, & M); getchar (); int I, j, X1, Y1, X2, Y2, X3, Y3, X4, Y4; memset (init. mat, 0, sizeof (init. mat); // clears each time! For (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) {scanf ("(% d, % d), (% d, % d) ", & X1, & Y1, & X2, & Y2, & X3, & Y3, & X4, & Y4); getchar (); If (I-1) * m + J = N * m) continue; // remove the outdegree init of the destination vertex. mat [(I-1) * m + J] [(x1-1) * m + Y1] = 1; init. mat [(I-1) * m + J] [(x2-1) * m + y2] = 1; init. mat [(I-1) * m + J] [(x3-1) * m + Y3] = 1; init. mat [(I-1) * m + J] [(x4-1) * m + Y4] = 1; unit. mat [(I-1) * m + J] [(I-1) * m + J] = 1;} mat MUL (MAT a, mat B) {int I, j, k; mat c; for (I = 1; I <= T; I ++) for (j = 1; j <= T; j ++) {C. mat [I] [J] = 0; For (k = 1; k <= T; k ++) // you cannot repeat the end point if you forget it at the beginning, to K = T c. mat [I] [J] = C. mat [I] [J] | (. mat [I] [k] & B. mat [k] [J]);} return C;} mat CAL (int K) // matrix binary fast power {mat I = init, ANS = unit; while (k) {If (K & 1) ans = MUL (ANS, I); I = MUL (I, I); k >>=1 ;} return ans ;} int main () {int t; scanf ("% d", & T); bool OK = 0; while (t --) {Init (); // input t = n * m; int num, Q; scanf ("% d", & num); // If (OK) puts (""); OK = 1; int I; while (Num --) {scanf ("% d", & Q); MAT r = CAL (Q); If (R. mat [1] [T] = 0) printf ("false \ n"); else {for (I = 1; I <= T; I ++) {If (R. mat [1] [I]) break;} if (I = T) printf ("True \ n"); else printf ("Maybe \ n ");}} puts ("");} return 0 ;}

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.