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 }
    功不成,身已退
  • 相关阅读:
    event.target.tagName
    Web安全之跨站伪造请求(CSRF)
    Web安全之XSS 入门与介绍
    Web安全之Web 安全介绍与基础入门知识
    设计模式之命令模式
    前端常用的库和实用技术之JavaScript多线程
    前端常用的库和实用技术之JavaScript面向切面编程
    设计模式之职责链模式
    设计模式之适配器模式
    设计模式之策略模式
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2604143.html
Copyright © 2011-2022 走看看