zoukankan      html  css  js  c++  java
  • HDOJ1142 A Walk Through the Forest

    A Walk Through the Forest

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


    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
     1 //最短路径+记忆化搜索
     2 #include<cstdio>
     3 #include<cstring> 
     4 int map[1001][1001];
     5 int dis[1001];
     6 int dp[1001];
     7 
     8 void dijkstra(int v,int n)
     9 {
    10     bool visit[1001];
    11     memset(visit,false,sizeof(visit));
    12     int i,j,k,min;
    13     visit[v]=true;
    14     for(i=1;i<n;++i)
    15     {
    16         min=0x3fffffff;
    17         k=1;
    18         for(j=1;j<=n;++j)
    19         {
    20             if(!visit[j]&&dis[j]<min)
    21             {
    22                 min=dis[j];
    23                 k=j;
    24             }
    25         }
    26         visit[k]=true;
    27         for(j=1;j<=n;++j)
    28         {
    29             if(!visit[j]&&dis[j]>map[k][j]+min)
    30                 dis[j]=map[k][j]+min;
    31         }
    32     }
    33 }
    34 
    35 int mDFS(int v,int n)//记忆化搜索
    36 {
    37     if(dp[v]!=-1)   //dp[i]中记录的是i到终点的所有可能路径的数量
    38         return dp[v];
    39     if(v==2)
    40         return 1;
    41     dp[v]=0;
    42     for(int i=1;i<=n;++i)
    43         if(map[i][v]!=0x3fffffff&&dis[i]<dis[v]) //记忆化搜索时基于这样的事实--我们利用Dijkstra找到的
    44             dp[v]+=mDFS(i,n);                    //从2号点到1号点的最短路径中的每个点v,都有dist[v]<dist[1]。    
    45     return dp[v];
    46 }
    47 
    48 int main()
    49 {
    50     int n,m,a,b,d,i,j;
    51     while(scanf("%d",&n),n)
    52     {
    53         scanf("%d",&m);
    54         for(i=1;i<=n;++i)
    55             for(j=1;j<i;++j)
    56                 map[i][j]=map[j][i]=0x3fffffff;
    57         memset(dp,-1,sizeof(dp));
    58         for(i=1;i<=m;++i)
    59         {
    60             scanf("%d%d%d",&a,&b,&d);
    61             map[a][b]=map[b][a]=d;
    62         }
    63         for(i=1;i<=n;++i)
    64             dis[i]=map[2][i];
    65         dijkstra(2,n);
    66         printf("%d\n",mDFS(1,n));
    67     }
    68     return 0;
    69 }
    功不成,身已退
  • 相关阅读:
    python爬虫-selenium八大定位笔记
    git pull : error: cannot lock ref 'refs/remotes/origin/*' (unable to update local ref) 解决方案
    Lua table.sort()原理和使用的坑
    Unity---有关游戏物体角度的两种赋值方法这件事
    C# #if、#endif和预处理指令
    Unity 4大坐标系 和 屏幕坐标与UI坐标的转换问题
    第2次参加游戏开发比赛
    Unity Text添加空格导致换行问题的两种解决方法(还有lua的解决方法)
    MySQL是如何处理千万级数据
    PHP使用守护进程处理队列
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2604143.html
Copyright © 2011-2022 走看看