hdu 1142 A Walk Through the Forest bfs+记忆化搜索

A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8705    Accepted Submission(s): 3221


Problem Description
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
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. 
 

Output
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
 

Sample Input
  
  
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
 

Sample Output
  
  
2 4
题意:

J老师的家在一片森林的一端,而他的工作地点则在另一端,最近他感到工作的压力很大,所以打算每天走路回家放松自己,因为路上可以在森林中看到很多美丽的景色,森林里有很多路口,所以他有很多中回家的路可以选择,但是他当然不想朝着离家越来越远的路走,如果一个路口距离家的距离中有比当前路口到家的所有路都要近的话,那么走到那个路口就是朝着离家近的方向走,J老师想知道他可以有多少中不同的回家的路。

Input

输入包括多组数据,以一个0结束,J老师将所有的路口或者路从1开始标号,他的工作地点标为1,家标为2,每组数据第一行给出路口数量N,1<N<=1000,然后是路的数量M,然后为M行每行三个整数a,b以及d,1<=d<=1000000,d表示从路口 a到路口b的距离,没两个路口间最多有一条路。

思路:这个题比较简单,先用bfs求出其他点到2的最短距离,然后在dfs记忆化搜。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<queue>
using namespace std;
#define INF 999999999;
struct edge
{
     int from,to;
     int w;
};
int n,m;
vector<int>G[1001];
vector<edge>edges;
int dis[1001];
int d[1001];
void addedge(int x,int y,int w)
{
    edge a={x,y,w};
    edges.push_back(a);
    edge b={y,x,w};
    edges.push_back(b);
    G[x].push_back(edges.size()-2);
    G[y].push_back(edges.size()-1);
}
void bfs()//bfs求最短路
{
    for(int i=1;i<=n;i++)
    {
        dis[i]=INF;
    }
    queue<int>q;
    q.push(2);
    dis[2]=0;
    while(!q.empty())
    {
        int a=q.front();
        q.pop();
        for(int i=0;i<G[a].size();i++)
        {
            edge b=edges[G[a][i]];
            if(dis[b.to]>dis[a]+b.w)
            {
                dis[b.to]=dis[a]+b.w;
                q.push(b.to);
            }

        }
    }

}
int dfs(int x)//记忆化求路的条数
{
    if(d[x]!=-1) return d[x];
    int count=0;
    for(int i=0;i<G[x].size();i++)
    {
        edge a=edges[G[x][i]];
        if(dis[a.to]<dis[x])
        {
            count+=dfs(a.to);
        }
    }
    d[x]=count;
    return d[x];
}
int main()
{
    while(scanf("%d",&n)&&n)
    {
      scanf("%d",&m);
      for(int i=1;i<=n;i++)
        G[i].clear();
      edges.clear();
      for(int i=1;i<=m;i++)
      {
          int x,y,w;
          scanf("%d %d %d",&x,&y,&w);
          addedge(x,y,w);
      }
      bfs();
      /*for(int i=1;i<=n;i++)
      {
          printf("%d ",dis[i]);
      }
      printf("\n");*/
      memset(d,-1,sizeof(d));
      d[2]=1;
      dfs(1);
      printf("%d\n",d[1]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值