FZU 1627 - Revival's road(矩阵快速幂)

 Problem 1627 Revival's road

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

4 5 91 22 33 44 11 34 5 11 22 33 44 11 3

 Sample Output

3 0

 Source

Summer Training Qualification II


题意:
问你在刚好k步时,有几条路线。

POINT:
这是矩阵快速幂的邻接矩阵的一个应用。

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值