首页 > ACM题库 > HDU-杭电 > HDU 1142 A Walk Through the Forest-记忆化搜索-[解题报告] C++
2013
11-28

HDU 1142 A Walk Through the Forest-记忆化搜索-[解题报告] C++

A Walk Through the Forest

问题描述 :

Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.

输入:

Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.

输出:

For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647

样例输入:

5 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1
7 5 1
6 7 1
5 2 1
6 2 1
0

样例输出:

2
4

惨烈了,,,自己题目都没有读懂..

晕死了..所以题目错了也就不稀奇了..

大家看看别人的代码吧,,我写的是错误的,,,算的是最短路径的数目..而题目中要求的不是,,具体题目中让求的什么.我也没看懂

下面是我的错误代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define inf 0x3fffffff

int N,M;//N represents the number of points,and M stands for the number of edges;

int map[1024][1024];

int cnt;

int ans;

int dis[1024];

int visit[1024];

const int sta=1;

const int end=2;

struct Edge{
	int v;
	int val;
	int next;
}e[1111111];

int head[1024];

int idx;

int dijkstra()
{
	memset(visit,0,sizeof(visit));
	for(int i=1;i<=N;i++)
		dis[i]=inf;
	dis[sta]=0;
	for(int j=1;j<=N;j++)
	{
		int t=inf;
		int pos;
		for(int i=1;i<=N;i++)
		{
			if(!visit[i]&&t>dis[i])
			{
				t=dis[i];
				pos=i;
			}
		}
		visit[pos]=1;
		for(i=1;i<=N;i++)
		{
			if(!visit[i]&&dis[i]>dis[pos]+map[pos][i]&&map[pos][i]!=0x3f3f3f3f)
			{
				dis[i]=dis[pos]+map[pos][i];
			}
		}
	}
	if(dis[end]==inf)
		return -1;
	else
		return dis[end];
}

void addedge(int a,int b,int val)
{
	e[idx].v=b;
	e[idx].val=val;
	e[idx].next=head[a];
	head[a]=idx++;
}

void DFS(int index,int val)
{
//	printf("%d___%d___\n",index,val);
	if(index==end)
	{
		if(val==ans)
		{
			cnt++;
			return;
		}
		else
			return;
	}
	if(val>=ans)
		return;
	else
	{
		for(int p=head[index];p!=-1;p=e[p].next)
		{
			int v=e[p].v;
			if(!visit[v])
			{
				visit[v]=1;
				DFS(v,val+e[p].val);
				visit[v]=0;
			}
		}
	}
}





int main()
{
	int aa,bb,cc;
	while(scanf("%d",&N)!=EOF,N)
	{
		memset(head,-1,sizeof(head));
		memset(map,0x3f,sizeof(map));
		scanf("%d",&M);
		int t=inf;
		idx=0;
		for(int i=1;i<=M;i++)
		{
			scanf("%d%d%d",&aa,&bb,&cc);
			if(map[aa][bb]>cc)//判断重边
			{
				map[aa][bb]=cc;
				map[bb][aa]=cc;
				addedge(aa,bb,cc);
				addedge(bb,aa,cc);
			}
		}
		ans=dijkstra();
	//	printf("%d____\n",ans);
		if(ans==-1)
			printf("0\n");
		else
		{
			cnt=0;
			memset(visit,0,sizeof(visit));
			visit[sta]=1;
			DFS(sta,0);
			printf("%d\n",cnt);
		}
	}
	return 0;
}

 

下面是别人的正确的代码

#include<stdio.h>
#include<stdlib.h>
int m,n,map[1024][1024],des[1024],dis[1024],dep[1024];
int inf = 0x7fffffff;
int Dijkstra( int p )
{
    dis[p] = 0;
    for( int i = 1; i <= n; ++i )
    {
         int min = inf, pos = 0,t;
         for( int j = 1; j <= n; ++j )
         {
              if( !des[j] )
                  if( dis[j] < min )
                  {
                      min = dis[j];
                      pos = j;
                  }
          }
          des[pos] = 1;
          for( int j = 1; j <= n; ++j )
          {
               if( !des[j] )
                   if( map[pos][j] != inf )
                       if( ( t = map[pos][j] + dis[pos] ) < dis[j] )
                           dis[j] = t;
           }
     }
     return 0;
}
int DFS( int x )
{
    
    if( dep[x] )//记忆化搜索
        return dep[x];
    if( x == 2 )
        return 1;
    for( int i = 1; i <= n ; ++i )
    {
         if( map[x][i] != inf )
             if( dis[i] < dis[x] )
                 dep[x] += DFS( i );
     }
     return dep[x];
}
int main( )
{
    while( scanf( "%d",&n ),n )
    {
           
           scanf( "%d",&m );
           for( int i = 0; i <= n; ++i )
           {
                for( int j = 0; j <= n; ++j )
                     map[i][j] = inf;
                des[i] = 0;
                dis[i] = inf;
                dep[i] = 0;
            }
            for( int i = 1; i <= m; ++i )
            {
                 int x,y,v;
                 scanf( "%d%d%d",&x,&y,&v );
                 map[x][y] = map[y][x] = v;
             }
            Dijkstra( 2 );
            int res = DFS( 1 );
            printf( "%d\n",res );
           }
          // system ("pause");
    return 0;
}

 


  1. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?

  2. “可以发现,树将是满二叉树,”这句话不对吧,构造的树应该是“完全二叉树”,而非“满二叉树”。