How many ways??
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2185 Accepted Submission (s): 820
Problem description Spring, HDU campus full of flowers, colorful, very beautiful. Onion is a love of flowers, watching the school grass race to open, walk the campus, the mood has become comfortable. In order to take a look at this fascinating campus, Onion decided, every class to go different routes to the classroom, but because of the time, each time only through K places, for example, the Onion decided after 2 places, he can first go to the square to look at the fountain, then to the classroom, you can go to the stadium a few laps, then to the classroom He was very interested to know that the number of programs from point A to the point of the K-point to B points, of course, this number can be very large, so you just output it modulo 1000 of the remainder. Can you help him? You can decide how many you can see in onions a day.
Input data has more than one group, the first row of each group is 2 integers n, m (0 < n <=, M <= 100) indicates that there are n points in the campus, for convenience, points from 0 to n-1 number, followed by M lines, each line has two integers s, t (0<=s, T<n) indicates that from the S point to the T point, note that the graph is forward. The next line is two integer t, which indicates that there is a T Group Inquiry (1<=T<=100),
The next T-line, each line has three integers a, B, K, which indicates that you can walk the repeating edge from point A to point B (K < 20) by the number of K points. If no such walk is present, the output 0
Enter end when n, M are all 0
Output calculates the number of scenarios per query, as a result of a large number of methods, outputs its 1000 modulo
Sample INPUT4 3 3 2 1 0Sample Output2013 analysis: When I see this problem using matrices to solve my first feeling is incredible, this with the matrix has what Chicken feather relationship?!! But when you look at the idea of the problem, the feeling is lying in the groove, the matrix incredibly can play, magical! Establish an adjacency matrix a[i][j] If there is a path, then C = A * A, the key is this, the matrix multiplied by the matrix equals the row column, assuming the first element as an example; c[0][0] = a[0][0] * a[0][0] + a[0][1]*a[1][0] +a[0][2]*a[2 ][0] ... The number of scenarios that are calculated from 0 to 0 is equal to (0 to 1, 1 to 0) + (0 to 2, 2 to 0) ..., this is just for two points, K is a^k
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5 using namespacestd;6 Const intMoD = +;7 structMat8 {9 intmat[ -][ -];Ten }; One intn,m; AMatoperator*(Mat X, Mat y) - { - Mat C; thememset (C.mat,0,sizeof(C.mat)); - for(intt =0; T < N; t++) - { - for(inti =0; I < n; i++) + { - for(intj =0; J < N; J + +) + { AC.MAT[I][J] + = x.mat[i][t]% mod * (y.mat[t][j)%MoD); atC.MAT[I][J]%=MoD; - } - } - } - returnC; - } inMatoperator^ (Mat x,intt) - { to Mat C; + for(inti =0; I < n; i++) - for(intj =0; J < N; J + +) theC.MAT[I][J] = (i = =j); * while(t) $ {Panax Notoginseng if(T &1) -c = c *x; thex = x *x; +T >>=1; A } the returnC; + } - intMain () $ { $ while(SCANF ("%d%d", &n, &m)! =EOF) - { - if(n = =0&& m = =0) the Break; - Mat a,temp;Wuyi intt,a,b,k; thememset (A.mat,0,sizeof(A.mat)); - while(m--) Wu { -scanf"%d%d", &A,&B); AboutA.MAT[A][B] =1; $ } -scanf"%d", &t); - while(t--) - { A for(inti =0; I < n; i++) + { the for(intj =0; J < N; J + +) -TEMP.MAT[I][J] =A.mat[i][j]; $ } thescanf"%d%d%d",&a,&b,&k); thetemp = temp ^K; theprintf"%d\n", Temp.mat[a][b]); the } - } in return 0; the}View Code
Hd2157how many Wasy?? (The application of the eight + adjacency matrices of the Ten matrix problems)