HDU 2157 How many ways?? (Fast power of the classical matrix)

Source: Internet
Author: User
Tags mul

Test instructions: Ask for a through the K-point to the number of Plan B

Method One:

a 0-1 matrix A
A[I][J] = 1 means I to J can reach or I to J there are 1 paths or I to J Pass a point of the scheme number of ways can be repeated

and A2 = A * A
The meaning of A[i][j] is
2-point scheme number from I to J

A K-square a[i,j] represents I to J Walk K-step scheme has A[I][J]

The definition of matrix multiplication is so consistent with this model that it is admirable to be very familiar with the concrete steps of matrix multiplication in order to abstract matrix multiplication in this topic to achieve all possible situations between two fixed points.

Method Two:

The idea of dynamic planning, the state is dp[c][u] from the starting point S to the U point through the C-side of all the method number, this point of view fixed, the end point as one of the dynamic planning ideas in the shortest path model has been seen. The decision after the DP (c,u) state is that there is a forward edge between the u,v, which can be transferred to the DP (C+1,V) state.


Matrix Fast Power:

1100 KB78 ms#include<cstdio> #include <iostream> #include <cstring> #include <algorithm>    using namespace std; #define MOD 1000struct mat{int a[25][25];    void Ini () {memset (a,0,sizeof (a));    }};int N,m;mat mapp;int St,ed,c;mat mul (Mat &m1,mat &m2) {mat ans;    Ans.ini (); for (int i=1;i<=n;i++) for (int. j=1;j<=n;j++) if (M1.a[i][j]) for (int k=1;k<=n;k++) ans.a[i][k]= (ans    . A[i][k]+m1.a[i][j]*m2.a[j][k])%mod; return ans;}    int Quickmul (Mat m,int c) {mat ans;    Ans.ini ();    for (int i=1;i<=n;i++) ans.a[i][i]=1;        while (c) {if (c&1) Ans=mul (ans,m);        M=mul (M,M);    c>>=1; } return ans.a[st][ed];}        int main () {while (scanf ("%d%d", &n,&m), n+m) {Mapp.ini ();            while (m--) {int u,v;            scanf ("%d%d", &u,&v);        Mapp.a[++u][++v]=1;        } int t;        scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &st,&ed,&AMP;C);            st++;ed++;        printf ("%d\n", Quickmul (Mapp,c)); }} return 0;}

Dynamic planning:

15MS 1092k#include<cstdio> #include <algorithm> #include <iostream> #include <cstring>using namespace std; #define MOD 1000int Dp[25][25];bool mapp[25][25];int Main () {    int n,m;    while (scanf ("%d%d", &n,&m), m+n) {        memset (MAPP));        while (m--) {            int u,v;            scanf ("%d%d", &u,&v);            mapp[++u][++v]=1;        }        int t;        scanf ("%d", &t);        while (t--) {            memset (dp,0,sizeof (DP));            int st,ed,k;            scanf ("%d%d%d", &st,&ed,&k);            st++;ed++;            Dp[0][st]=1;            for (int c=1;c<=k;c++) for (int            u=1;u<=n;u++) for            (int v=1;v<=n;v++) {                if (Mapp[u][v]) Dp[c][v] = (Dp[c][v]+dp[c-1][u])%mod;            }            printf ("%d\n", dp[k][ed]);        }    }    return 0;}


HDU 2157 How many ways?? (Fast power of the classical matrix)

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.