HDU 1142 A Walk Through the Forest
该题是一道记忆化搜索,这题与HDU 1248 漫步校园是一样的,这里不解释了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int inf=0x7fffffff;
struct t
{
int x,y;
}q[1000024];
int n,m,hash[1024],dis[1024],map[1024][1024];
void empty()
{
for( int i=0;i<=n;i++ )
{
hash[i]=0;
dis[i]=inf;
for( int j=0;j<=n;j++ )
map[i][j]=inf;
}
}
void DIJ( )
{
dis[2]=0;
for( int i=1;i<=n;i++ )
{
int t=inf,pos;
for( int j=1;j<=n;j++ )
{
if( t>dis[j]&&!hash[j] )
{
t=dis[j];
pos=j;
}
}
hash[pos]=1;
for( int j=1;j<=n;j++ )
{
if( !hash[j]&&map[pos][j]!=inf )
{
if( dis[j]>map[pos][j]+dis[pos] )
dis[j]=map[pos][j]+dis[pos];
}
}
}
}
int DFS( int x )
{
if( x==2 ) return 1;
if( hash[x]>0 ) return hash[x];
for( int i=1;i<=n;i++ )
{
if( map[x][i]!=inf&&dis[x]>dis[i] )
hash[x]+=DFS( i );
}
return hash[x];
}
int main( )
{
int x,y,t;
while( scanf( "%d",&n ),n )
{
scanf( "%d",&m );
empty( );
for( int i=0;i<m;i++ )
{
scanf( "%d%d%d",&x,&y,&t );
if( map[x][y]>t )
map[x][y]=map[y][x]=t;
}
DIJ( );
memset( hash,0,sizeof( hash ) );
DFS( 1 );
printf( "%d\n",hash[1] );
}
return 0;
}