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

    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,如果从a点到2的距离大于从b点到2的距离,并且a能到b(即两点间有边(路)),那么就从a走到b。问这样的路有几条。Dijkstra+记忆化搜索!
    AC代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int MAXN = 1005;
     5 int n,m,a,b,c,dis[MAXN],pis[MAXN],G[MAXN][MAXN];
     6 bool vis[MAXN];
     7 void Dijkstra()
     8 {
     9     for(int i=1;i<=n;i++)//把终点2当成起点,求出起点2到各节点的最短路径
    10         dis[i]=G[2][i];
    11      dis[2]=0;vis[2]=true;
    12      for(int i=1;i<n;i++){
    13         int k=-1;
    14         for(int j=1;j<=n;j++)
    15             if(!vis[j] && (k==-1||dis[j]<dis[k]))k=j;
    16         if(k==-1)break;
    17         vis[k]=true;
    18         for(int j=1;j<=n;j++)
    19              if(!vis[j])dis[j]=min(dis[j],dis[k]+G[k][j]);
    20     }
    21 }
    22 int dfs(int x){//记忆化搜索
    23     if(pis[x]!=-1)return pis[x];
    24     if(x==2)return 1;
    25     pis[x]=0;
    26     for(int i=1;i<=n;++i)//x到i有路且x到终点距离大于i到终点距离
    27         if(G[x][i]!=INF && dis[x]>dis[i])pis[x]+=dfs(i);
    28     return pis[x];
    29 }
    30 int main()
    31 {
    32     while(~scanf("%d",&n) && n){
    33         scanf("%d",&m);
    34         for(int i=1;i<=n;++i)
    35             for(int j=1;j<=n;++j)
    36                 G[i][j]=(i==j?0:INF);
    37         memset(vis,false,sizeof(vis));
    38         memset(pis,-1,sizeof(pis));
    39         for(int i=1;i<=m;++i){
    40             scanf("%d %d %d",&a,&b,&c);
    41             G[a][b]=G[b][a]=c;
    42         }
    43         Dijkstra();
    44         printf("%d
    ",dfs(1));
    45     }
    46     return 0;
    47 }
     
  • 相关阅读:
    每日一题计划
    acm新手刷题攻略之poj
    Swift几行代码设置UIcollectionView的section底色,圆角
    简单几行代码设置UIcollectionView底色、section背景底色、背景色、背景阴影、背景圆角,支持CollectionView内容左对齐、居中对齐、右对齐、右对齐且右开始排序,支持底色点击反馈
    iOS12 EachNavigationBar导航栏操作出现黑边解决办法
    EachNavigationBar 导航栏颜色与给定颜色不相同设定
    详解冒泡排序法
    递归的简单用法
    判断一个整数是否为素数(质数)
    tcp黏包与拆包
  • 原文地址:https://www.cnblogs.com/acgoto/p/9048924.html
Copyright © 2011-2022 走看看