A Walk Through the Forest

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

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
 

从1号点走到二号点,下一步到2号点永远要比上一步离2号点近(比如第一个测试样例中2-3 为37 1-2为36,所以肯定不会走3号点)。求符合这样的路径条数。

先以2号点做迪杰斯特拉,求出它到每个点的最小距离,然后记忆化搜索。
做了这个题,我推荐还可以做下 hdu 1428
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <math.h>
  6. using namespace std;
  7. const int N = ;
  8. const int INF = ;
  9. int graph[N][N];
  10. int n,m;
  11. bool vis[N];
  12. int low[N];
  13. int dp[N];
  14. void dijkstra(int n,int pos)
  15. {
  16. memset(vis,,sizeof(vis));
  17. vis[pos]=;
  18. for(int i=; i<=n; i++) low[i]=graph[pos][i];
  19. low[pos] = ;
  20. for(int i=; i<n; i++)
  21. {
  22. int Min=INF;
  23. for(int j=; j<=n; j++)
  24. if(vis[j]==&&Min>low[j])
  25. {
  26. pos=j;
  27. Min=low[j];
  28. }
  29. vis[pos]=;
  30. for(int j=; j<=n; j++)
  31. if(vis[j]==&&low[j]>graph[pos][j]+low[pos])
  32. {
  33. low[j]=graph[pos][j]+low[pos];
  34. }
  35. }
  36. }
  37. int dfs(int s,int n){
  38. if(dp[s]>) return dp[s];
  39. int ans = ;
  40. for(int i=;i<=n;i++){
  41. if(graph[s][i]<INF&&low[s]>low[i]&&i!=s){
  42. ans+=dfs(i,n);
  43. }
  44. }
  45. dp[s] = ans;
  46. return dp[s];
  47. }
  48. int main()
  49. {
  50. while(scanf("%d",&n)!=EOF,n)
  51. {
  52. scanf("%d",&m);
  53. for(int i=;i<=n;i++){
  54. for(int j=;j<=n;j++) graph[i][j]=INF;
  55. }
  56. for(int i=; i<=m; i++)
  57. {
  58. int a,b,c;
  59. scanf("%d%d%d",&a,&b,&c);
  60. graph[a][b]=graph[b][a] = c;
  61. }
  62. dijkstra(n,);
  63. memset(dp,,sizeof(dp));
  64. dp[]=;
  65. printf("%d\n",dfs(,n));
  66. }
  67. }
 

hdu 1142(迪杰斯特拉+记忆化搜索)的更多相关文章

  1. HDU 1176 免费馅饼(记忆化搜索)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. HDU 1428 漫步校园(记忆化搜索,BFS, DFS)

    漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...

  3. hdu 4111 Alice and Bob 记忆化搜索 博弈论

    Alice and Bob Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  4. hdu 1176免费馅饼(记忆化搜索)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 题意不解释了 简单的记忆化搜索可以拿来练练手,注意要从pos = 5 开始搜索 #include ...

  5. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  6. HDU 2476 String painter(记忆化搜索, DP)

    题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...

  7. hdu 5389 Zero Escape(记忆化搜索)

    Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi ...

  8. HDU - 1078 FatMouse and Cheese (记忆化搜索)

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

  9. hdu 1978 How many ways 记忆化搜索 经典例题

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. 《Cracking the Coding Interview》——第17章:普通题——题目6

    2014-04-28 22:49 题目:给定一个整数数组.如果你将其中一个子数组排序,那么整个数组都变得有序.找出所有这样子数组里最短的一个. 解法:线性时间,常数空间内可以解决,思想类似于动态规划. ...

  2. Pascal小游戏 双人射击

    一个双人的游戏 Pascal源码附上 只要俩人不脑残,一下午玩不完...又是控制台游戏中的一朵奇葩. Free Pascal 射击游戏 Program shooting_game; uses crt; ...

  3. Bit与Byte的区别

    在工作中遇到一些概念模糊的地方, 需要记住了bit意为“位”或“比特”,是计算机运算的基础: byte意为“字节”,是计算机文件大小的基本计算单位: 说到usb2.0标准接口传输速率.许多人都将“48 ...

  4. Jetty 安装、启动与项目部署

    Jetty是当下非常流行的一款轻量级Java Web服务器和Servlet容器实现,它由Eclipse基金会托管,完全免费而且开放源代码,因此所有人均可以从其官网下载最新源代码进行研究.由于其轻量.灵 ...

  5. mybatis批量添加、批量删除

    <!-- 批量添加 --> <insert id="insertNameListSynHisBatch" parameterType="java.uti ...

  6. django-settings里mysql连接配置

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'dailyfresh', 'HOST': 'loca ...

  7. virsh command

    http://www.cnblogs.com/chenjiahe/category/845519.html resize qemu-img resize win2k8r2.qcow2 40G conv ...

  8. Set(), Get() 真正的目的

    在各种面向对象编程中,都有 Set(), Get() 两种方法. 1 常见理解 1 为了保证安全性 2 为了规范代码 其实这些理解都是对的.具体看我们从哪个角度去理解这个内容. 2 个人理解 2.1 ...

  9. hadoop-hdfs(三)

    HDFS概念 1 数据块* HDFS的一个数据块默认是64M,与元数据分开管理. 优点: 数据块的大小设计的较大,所以寻址占传输的时间比例较小,只需要计算传输速度即可. 便于简化管理,利于计算剩余空间 ...

  10. hdu 1564 Play a game (博弈)

    Play a game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...