Accept: 209 Submit: 427
Time Limit: 5000 mSec Memory Limit : 32768 KB
Problem 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
Sample Output
Source
Summer Training Qualification II#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define LL long long
const int maxn = 111+43;
const int mod = 10000;
int n,m;
struct node
{
int mp[maxn][maxn];
};
node cheng(node a,node b)
{
node c;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
c.mp[i][j]=0;
for(int k=1;k<=n;k++){
(c.mp[i][j]+=a.mp[i][k]%mod*b.mp[k][j]%mod)%=mod;
}
}
}
return c;
}
void jzqkm(node base,int mi)
{
node ans;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) ans.mp[i][j]=1;
else ans.mp[i][j]=0;
}
}
while(mi){
if(mi&1)
ans=cheng(ans,base);
base=cheng(base,base);
mi=mi/2;
}
printf("%d\n",ans.mp[1][n]%mod);
}
int main()
{
int k;
while(~scanf("%d%d%d",&n,&m,&k)){
node a;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
a.mp[i][j]=0;
}
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
a.mp[u][v]++;
}
jzqkm(a,k);
}
return 0;
}