windy在有向图中迷路了。 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1。 现在给出该有向图,你能告诉windy总共有多少种不同的路径吗? 注意:windy不能在某个节点逗留,且通过某有向边的时间严格为给定的时间。
标签:
跟上一题差不多,将每个点拆成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;
}
windy在有向图中迷路了。 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1。 现在给出该有向图,你能告诉windy总共有多少种不同的路径吗? 注意:windy不能在某个节点逗留,且通过某有向边的时间严格为给定的时间。
第一行包含两个整数,N T。 接下来有 N 行,每行一个长度为 N 的字符串。 第i行第j列为‘0‘表示从节点i到节点j没有边。 为‘1‘到‘9‘表示从节点i到节点j需要耗费的时间。
包含一个整数,可能的路径数,这个数可能很大,只需输出这个数除以2009的余数。
30%的数据,满足 2 <= N <= 5 ; 1 <= T <= 30 。 100%的数据,满足 2 <= N <= 10 ; 1 <= T <= 1000000000 。
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5699247.html