1482 Route Statistics

Source: Internet
Author: User

1482 Route Statistics

time limit: 1 sspace limit: 256000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description Description

A graph of n nodes, which is the total number of scenarios from start to finish just over time. MoD 502630.

Enter a description Input Description

The first line contains an integer n, and all points are numbered from 0 to N-1.

Next n rows, each row contains n characters. The J-Character of line I indicates the time required for I to J. Characters can only be ' 1 ' to ' 5 ', or '. ' Indicates that I cannot reach J. Ensure that the main diagonal is '. '.

Next line 3 integers start, finish, time.

Output description Output Description

The total number of output scenarios.

Sample input Sample Input





0 2 5

Sample output Sample Output


Data range and Tips Data Size & Hint

For 20% of the data, the character entered is not ' 1 ' is '. ';

For 100% data, 1 <= n <= 10; 1 <= start,finish <= N; 1 <= time <= 10^9.

Category labels Tags Click here to expandMatrix multiplication Number Theory

"Problem Solving Report"

First glance, DFS, but look at the range of T, apparently timed out.

Again, the point is very small (the matrix of the N power time-consuming less), a lot of time (go complex)

The symbol of matrix multiplication Ah!!!

We know the infinity graph from S through K steps to f how to ask: that is 01 matrix:

Establish matrix A when and only if there is an edge i->j, a (i,j) = 1.

So c=a*a, then C (i,j) =σa (i,k) *a (k,j), is actually equal to the number of paths from point I to J that passes exactly 2 edges (enumeration K is the transit point)

Similarly, the first row J of C*a indicates the number of paths from I to J through 3 edges.

Similarly, if the number of paths through the K-step is required, we only need two points to find out the a^k.

But the figure has the edge, how to do?

Two characters: split!

The relationship between each point is stored in a matrix, I can be 1 steps to J Mark 1, not to mark 0, note the right side of the title is 1-5, then can be split, each point is split into a point of edge,

But that's not enough, we're going to build (n*n*5) ^2 = 500*500 matrix, matrix multiplication T reaches 500 ^ 3 This is obviously too much.

So: We encountered a side i,j, the right to C, it was split into i–> i+n*1-i+n*2->, ..... i+n* (C-1), J


So there is only (n*5) the matrix of ^2=50*50.

The matrix becomes the 01 matrix when the point is removed

The a[i][j in this moment] saves 1 steps from I to J, requiring T-step, then directly squared the matrix to the T-time answer.

AC Code:

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMod=502630;intF,t,s,n;CharC;intans;intCNT;structnode{intf[ -][ -];} e,x;voidClean () { for(intI=0; i<=n;i++) e.f[i][i]=1;} Node Cheng (node A,node b) {//matrix multiplicationnode NE;  for(intI=0; i<n;i++){         for(intj=0; j<n;j++) {Ne.f[i][j]=0;  for(intk=0; k<n;k++) ne.f[i][j]= (ne.f[i][j]+ (Long Long) a.f[i][k]*b.f[k][j])%mod)%MoD; }    }    returnNE;}intAnswer () {node NE=x; Node=D; intb=T;  while(b) {//Matrix exponentiation        if(b&1) ass=Cheng (ass,ne); b>>=1; NE=Cheng (ne,ne); }    returnass.f[s][f];}intMain () {scanf ("%d\n",&N); CNT=N;  for(intI=0; i<n;i++){         for(intj=0; j<n;j++) {scanf ("%c",&c); if(c>='0'&&c<='9') {CNT=c-'0';  for(intk=1; k<cnt;k++) x.f[n* (K-1) +i][n*k+i]=1;//Split Pointx.f[n* (cnt-1) +i][j]=1; }} scanf ("\ n"); } N*=5;//last N to multiply by 5scanf"%d%d%d",&s,&f,&t);                    Clean (); //Defining the Unit matrixprintf"%d\n", Answer ()); return 0;}

1482 Route Statistics

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.