天天看點

FZU 1627 Revival's road

Description

Oaiei is idle, and recently he wants to travel around the country. In his country there are N cities, they are numbered from 1 to N. There are roads between some cities, but some are not directly connected with each other. Oaiei lives in the city 1, and he wants to go to city N. Now, he has a question. In k steps, he would like to know there are how many ways from city 1 to city N. You should note that althought there is a road between city A and city B, there is not necessarily a road between city B and city A.

Input

There are multiple tests. For each test, the first line contains three integers N、M and k(2<=N<=100,1<=M<=N*N,1<=k<=10^9), N denotes the number of cities, M denotes the number of roads between the N cities. Following M lines, each line contains two integers A and B, denoting there is a road between city A and city B.

Output

There are how many ways from city 1 to city N. Becase the answer can be very large, you should output the answer MOD 10000.

Sample Input

4 5 9

1 2

2 3

3 4

4 1

1 3

4 5 1

1 2

2 3

3 4

4 1

1 3

Sample Output

3

求k秒之後在n的方案數,對于圖來說,求方案數等同于進行矩陣乘法,所有直接快速幂即可。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 10000;
const int N = 101;
const int read()
{
  char ch = getchar();
  while (ch<'0' || ch>'9') ch = getchar();
  int x = ch - '0';
  while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
  return x;
}
int T, n, m, q, x, y;
int f[N][N], c[N][N], a[N];

int main()
{
  while (scanf("%d%d%d", &n, &m, &q) != EOF)
  {
    memset(f, 0, sizeof(f));
    memset(a, 0, sizeof(a));  
    a[1] = 1;
    rep(i, 1, m) scanf("%d%d", &x, &y), f[x][y] = 1;
    for (; q; q >>= 1)
    {
      if (q & 1)
      {
        rep(i, 1, n)
        {
          c[1][i] = 0;
          rep(j, 1, n) (c[1][i] += a[j] * f[j][i]) %= mod;
        }
        rep(i, 1, n) a[i] = c[1][i];
      }
      rep(i, 1, n) rep(j, 1, n)
      {
        c[i][j] = 0;
        rep(k, 1, n) (c[i][j] += f[i][k] * f[k][j]) %= mod;
      }
      rep(i, 1, n) rep(j, 1, n) f[i][j] = c[i][j];
    }
    printf("%d\n", a[n]);
  }
  return 0;
}      

FAQ | About | 

​​Google Group​​ |