[Matrix multiplication] JZOJ 2288 crocodile swamp

Description

  Pantanal wetlands known as the world's largest piece of wetland, which is located in the southern region of Mato Grosso in central Brazil. During the rainy season comes, this blue waves, vibrant, attracting many tourists.
  To make the play more interesting, the people at the center of the pond and the construction of several stone bridge pier, every stone bridge connects two piers, and between each two piers at most only a stone bridge. This attraction after construction has dared open because of the pond, there are many dangerous piranha.
  Mr. Bean loves adventure, he heard the news, immediately rushed to the pond, wanted to travel on the bridge of the first person. Although the Peas love adventure, but do not dare cross their own life a joke, and he began a careful field survey, and got some surprising conclusions: the route of piranhas is cyclical, this period may only be 2, 3 or 4 per unit time. Per unit of time, piranhas can swim from one pier to another pier. Each to a pier, if someone above it will attack, or continue its periodic motion. Without the pier, it is not going to attack people.
  With advanced equipment, Peas soon find out the movement of all of the piranha, he began to design their own course of action. Per unit time, he can only come from one pier to another pier along the stone bridge, could not stop at the pier on a seat does not move, because standing still will have other hazards. If Peas and a piranhas at the same time to reach the seat of a pier, they were piranhas attack, he certainly did not want that to happen.
  Now Peas have already selected two piers Start and End, he wanted to start from the Start, after K units of time just standing on the pier End. Assuming that the pier can be repeated through (including the Start and End), he would like you to help calculate, the total number of species of this route (of course, not been piranhas attack).
 

Input

  Total input file row M + 2 + NFish.
  The first line contains five positive integers N, M, Start, End, and K, respectively, the number of piers needed, the number of stone, the Start and End pier and a pier route number per unit time. Pier integer number of 0 to N-1.
  M + 1 to the second line, to give information stone. Each row two integers x and y, 0 ≤ x, y ≤ N-1, represents the number of this stone bridge the two x and y pier.
  M + 2 of the line is an integer NFish, represents the number of piranha.
  The first to M + 3 M + 2 + NFish rows, each row gives information about a piranha. The first row is each integer T, T = 2,3 or 4, represents a piranha motion cycles. Then there is the number of T, represents the travel route within a period Piranha.
  If T = 2, the number 2 has the next P0 and P1, piranha from P0 to P1, from P1 to P0, ......;
  if T = 3, there are three next number P0, P1 and P2, from Piranha P0 to P1, from P1 to P2, from P2 to P0, ......;
  if T = 4, the number of the next 4 P0, P1, P2 and P3, piranha from P0 to P1, from P1 to P2, from P2 to P3, from P3 to P0, .......
  When all the piranhas are starting Peas P0 position on their route, please be assured that this position will not Start the pier.

Output

  Output of several routes, as this number may be large, as long as you output the remainder divided by 10,000 on the line.
 

Sample Input

6 8 1 5 3
0 2
2 1
1 0
0 5
5 1
1 4
4 3
3 5
1
3 0 5 1

Sample Output

2
 

Data Constraint

 
 

Hint

Conventions []
  . 1 ≤ N ≤ 50
  . 1 ≤ K ≤ 2,000,000,000
  . 1 ≤ ≤ 20 is NFish

analysis

I always felt when the examination questions and then you saw them ...... like a good approach is to think of it after the matrix multiplication exercises of J Lord

We can find moments multiplication process is actually a process similar to floyd, you can put the path one point to another point total grows out, so is the number of programs in the case of the edge weights of 1

Then a look at the big K can probably guess that moment take, but do not cry food fight

Because the fish are circulating section, we can put the least common multiple cycle power section as a quick reference

A first matrix T from a process where 1 to 12 all the time are multiplied together

Then quickly power ^ k / 12

Finally, multiply-by-k matrix T to 12%

 

#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
typedef long long ll; 
const int N=60;
const ll P=1e4;
int n,fish,m,st,ed,K,fp[N][N];
bool use[N];
struct Rect {
    ll a[N][N];
    ll *operator [](int i){return a[i];}
    void In() {
        for (int i=0;i<n;i++) a[i][i]=1;
    }
    void Empty() {memset(a,0,sizeof a);}
    Rect operator * (Rect b) {
        Rect c;c.Empty();
        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
                for (int k=0;k<n;k++)
                    if (!use[k])
                        (c[i][j]+=a[i][k]*b[k][j]%P)%=P;
        return c;
    }
} F years, e;

Rect Ksm(Rect a,int y) {
    Rect years;
    memset(use,0,sizeof use);ans.In();
    while (y) {
        if (y&1) ans=ans*a;
        a=a*a;
        y>>=1;
    }
    return years;
}

void Pre_Process(int x) {
    memset(use,0,sizeof use);
    for (int i=1;i<=fish;i++) use[fp[i][(x-1)%fp[i][0]+1]]=1;
}

int main () {
    scanf("%d%d%d%d%d",&n,&m,&st,&ed,&K);e.In();ans.In();
    for (int i=1,u,v;i<=m;i++) scanf("%d%d",&u,&v),f[u][v]++,f[v][u]++;
    scanf("%d",&fish);
    for (int i=1;i<=fish;i++) {
        scanf("%d",&fp[i][0]);
        for (int j=1;j<=fp[i][0];j++) scanf("%d",&fp[i][j]);
    }
    for (int i=1;i<=12;i++) Pre_Process(i),e=e*f;
    ans=ans*Ksm(e,K/12);
    for (int i=1;i<=K%12;i++)
        Pre_Process (i) years = years * f;
    printf("%lld",ans.a[st][ed]);
}
View Code

 

Guess you like

Origin www.cnblogs.com/mastervan/p/11145012.html