zoukankan      html  css  js  c++  java
  • HDOJ1142 A Walk Through the Forest[DFS(记忆化搜索)+Dijkstra]

    A Walk Through the Forest

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


    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
     
    Source
     
    Recommend
    Eddy

    记忆化搜索

    code:

      1 #include <iostream>   
      2 #include <iomanip>   
      3 #include <fstream>   
      4 #include <sstream>   
      5 #include <algorithm>   
      6 #include <string>   
      7 #include <set>   
      8 #include <utility>   
      9 #include <queue>   
     10 #include <stack>   
     11 #include <list>   
     12 #include <vector>   
     13 #include <cstdio>   
     14 #include <cstdlib>   
     15 #include <cstring>   
     16 #include <cmath>   
     17 #include <ctime>   
     18 #include <ctype.h> 
     19 using namespace std;
     20 
     21 #define MAXN 1005
     22 #define intmax 9999999
     23 
     24 int map[MAXN][MAXN];
     25 int dis[MAXN];
     26 int sum[MAXN];
     27 int n,m;
     28 int cnt;
     29 
     30 void init()
     31 {
     32     int i,j;
     33     for(i=0;i<=n;i++)
     34         for(j=i;j<=n;j++)
     35             map[i][j]=map[j][i]=intmax;
     36 }
     37 
     38 void Dijkstra()
     39 {
     40     bool vst[MAXN];
     41     for(int i=1;i<=n;i++)
     42     {
     43         vst[i]=0;
     44         dis[i]=map[2][i];
     45     }
     46     dis[2]=0;
     47     vst[2]=1;
     48     for(int i=2;i<=n;i++)
     49     {
     50         int temp=intmax;
     51         int u=2;
     52         for(int j=1;j<=n;j++)
     53             if(dis[j]<temp&&!vst[j])
     54             {
     55                 temp=dis[j];
     56                 u=j;
     57             }
     58         vst[u]=1;
     59         for(int j=1;j<=n;j++)
     60         {
     61             if(map[u][j]<intmax)
     62             {
     63                 int update=dis[u]+map[u][j];
     64                 if(update<dis[j])
     65                     dis[j]=update;
     66             }
     67         }
     68     }
     69 }
     70 
     71 int dfs(int v)
     72 {
     73     int i;
     74     if(sum[v])                               //记忆化搜索
     75         return sum[v];                       //sum[i]表示从i到终点的所有可能路径
     76     if(v==2)
     77         return 1;
     78     for(i=1;i<=n;i++)
     79     {
     80         if(map[v][i]<intmax&&dis[i]<dis[v])
     81         {
     82             sum[v]+=dfs(i);
     83         }
     84     }
     85     return sum[v];
     86 }
     87 
     88 int main()
     89 {
     90     int i,j;
     91     int a,b,len;
     92     int dist;
     93     while(~scanf("%d",&n),n)
     94     {
     95         scanf("%d",&m);
     96         init();
     97         while(m--)
     98         {
     99             scanf("%d%d%d",&a,&b,&len);
    100             if(map[a][b]>len)
    101                 map[a][b]=map[b][a]=len;
    102         }
    103         Dijkstra();
    104         memset(sum,0,sizeof(sum));
    105         printf("%d\n",dfs(1));
    106     }
    107     return 0;
    108 }
  • 相关阅读:
    AES密码算法详解(转自https://www.cnblogs.com/luop/p/4334160.html)
    快速排序和插入排序——我的代码
    北京大学1001ACM——高精度类型题总结
    C语言数组不知道输入几个整数以及输入一直到为0
    C语言并查集例子——图问题巧用parent[]数组
    C语言快速判断素数——不超时
    C语言如何才能使用bool类型
    C语言两个特别大的整数类型相加超出范围使用两个技巧
    C语言存30位数字长的十进制方法
    dockerfile相关命令
  • 原文地址:https://www.cnblogs.com/XBWer/p/2631797.html
Copyright © 2011-2022 走看看