码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1297: [SCOI2009]迷路

时间:2016-07-23 19:39:57      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

跟上一题差不多,将每个点拆成9个点就可以了。(不过跑的有点慢

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-‘0‘,c=getchar();
	return x;
}
const int mod=2009;
const int nmax=95;
const int inf=0x7f7f7f7f;

int N,M,T;char S[20];
struct matrix{
	int a[nmax][nmax];
	matrix(){
		clr(a,0);
	}
	matrix operator*(const matrix &o)const{
         matrix tmp;
		 REP(i,1,M) REP(j,1,M) REP(k,1,M) if((tmp.a[i][j]+=a[i][k]*o.a[k][j])>=mod) ;
		 return tmp;	
	}
}a,b;
int id(int x,int y){
	return (x-1)*9+y;
}
int main(){
	N=read(),T=read(),M=N*9;
	REP(i,1,N){
		scanf("%s",S);
		REP(j,1,N) if(S[j-1]-‘0‘>0) b.a[id(i,S[j-1]-‘0‘)][id(j,1)]=1;
	}
	REP(i,1,N) REP(j,1,8) b.a[id(i,j)][id(i,j+1)]=1;
	REP(i,1,M) a.a[i][i]=1;
	
	
	while(T) {
		if(T&1) a=a*b;
		b=b*b;T>>=1;
	}
	printf("%d\n",a.a[1][id(N,1)]);
	return 0;
}

  

1297: [SCOI2009]迷路
Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1036  Solved: 723
[Submit][Status][Discuss]

Description

windy在有向图中迷路了。 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1。 现在给出该有向图,你能告诉windy总共有多少种不同的路径吗? 注意:windy不能在某个节点逗留,且通过某有向边的时间严格为给定的时间。

Input

第一行包含两个整数,N T。 接下来有 N 行,每行一个长度为 N 的字符串。 第i行第j列为‘0‘表示从节点i到节点j没有边。 为‘1‘到‘9‘表示从节点i到节点j需要耗费的时间。

Output

包含一个整数,可能的路径数,这个数可能很大,只需输出这个数除以2009的余数。

Sample Input

【输入样例一】
2 2
11
00

【输入样例二】
5 30
12045
07105
47805
12024
12345


Sample Output

【输出样例一】
1

【样例解释一】
0->0->1

【输出样例二】
852

HINT

 

30%的数据,满足 2 <= N <= 5 ; 1 <= T <= 30 。 100%的数据,满足 2 <= N <= 10 ; 1 <= T <= 1000000000 。

 

Source

 
[Submit][Status][Discuss]

bzoj1297: [SCOI2009]迷路

标签:

原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5699247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!