很神奇的4720msAC....
看来double 不是一般的慢啊!!哎呀呀~
#include<iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#define eps 1e-5
using namespace std;struct node
{ double m[222][222];
}res,temp,mod;int n,m,t;node matriXmult( node a,node b )
{node c;memset( c.m,0,sizeof(c.m) );for( int i=0;i<n;i++ )for( int k=0;k<n;k++ )if( a.m[i][k] )for( int j=0;j<n;j++ ){if( b.m[k][j] )c.m[i][j]+=a.m[i][k]*b.m[k][j];}return c;
}
void matrix_Power()
{//memset( res.m,0,sizeof(res.m) );//memset( temp.m,0,sizeof(temp.m) );for( int i=0;i<n;i++ ){ for( int j=0;j<n;j++ )temp.m[i][j]=mod.m[i][j],res.m[i][j]=i==j;}for( int i=0;m>=(1<<i);i++ ){if( m&(1<<i) )res=matriXmult(res,temp);temp=matriXmult(temp,temp);}
}
int main()
{int date[222];while( scanf("%d%d",&n,&m)!=EOF ){ if( n==0&&m==0 )break;for( int i=0;i<n;i++ )scanf( "%d",&date[i] );memset( mod.m,0,sizeof(mod.m) );for( int i=0;i<n;i++ )mod.m[i][i]=1;scanf( "%d",&t );int a,b;double rate;for( int i=0;i<t;i++ ){scanf( "%d%d%lf",&a,&b,&rate );mod.m[a][a]-=rate;mod.m[a][b]+=rate;}matrix_Power();double ans=0;for( int i=0;i<n;i++ )ans+=date[i]*res.m[i][n-1];printf( "%.0lf\n",ans );}
}
下面的代码优化了一下,果然不用结构体还是十分的省时间的啊!
下面的代码Time:470ms,小成就啊!
#include<iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#define eps 1e-12
using namespace std;double res[201][201],mod[201][201];int n,m,t;
void matriXmult( double a[][201],double b[][201] )
{int i,j,k;double c[201][201];for( int i=0;i<n;i++ )for( int j=0;j<n;j++ )c[i][j]=0;for( int i=0;i<n;i++ )for( int k=0;k<n;k++ )if( a[i][k]>eps )for( int j=0;j<n;j++ )c[i][j]+=a[i][k]*b[k][j];for( int i=0;i<n;i++ )for( int j=0;j<n;j++ )a[i][j]=c[i][j];
}void matrix_Power(double res[][201],int m)
{if( m==1 ){for( int i=0;i<n;i++ )for( int j=0;j<n;j++ )res[i][j]=mod[i][j];return ;}matrix_Power( res,m>>1 );matriXmult( res,res );if( m&1 )matriXmult( res,mod );
}int main()
{int date[222];while( scanf("%d%d",&n,&m)!=EOF ){ if( n==0&&m==0 )break;for( int i=0;i<n;i++ )scanf( "%d",date+i );for( int i=0;i<n;i++ )for( int j=0;j<n;j++ )mod[i][j]=i==j;scanf( "%d",&t );int a,b;double rate;for( int i=0;i<t;i++ ){scanf( "%d%d%lf",&a,&b,&rate );mod[a][a]-=rate;mod[a][b]+=rate;}matrix_Power(res,m);double ans=0;for( int i=0;i<n;i++ )ans+=date[i]*res[i][n-1];printf( "%.0lf\n",ans );}return 0;
}