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
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const int INF = ;
int graph[N][N];
int n,m;
bool vis[N];
int low[N];
int dp[N];
void dijkstra(int n,int pos)
{
memset(vis,,sizeof(vis));
vis[pos]=;
for(int i=; i<=n; i++) low[i]=graph[pos][i];
low[pos] = ;
for(int i=; i<n; i++)
{
int Min=INF;
for(int j=; j<=n; j++)
if(vis[j]==&&Min>low[j])
{
pos=j;
Min=low[j];
}
vis[pos]=;
for(int j=; j<=n; j++)
if(vis[j]==&&low[j]>graph[pos][j]+low[pos])
{
low[j]=graph[pos][j]+low[pos];
}
}
}
int dfs(int s,int n){
if(dp[s]>) return dp[s];
int ans = ;
for(int i=;i<=n;i++){
if(graph[s][i]<INF&&low[s]>low[i]&&i!=s){
ans+=dfs(i,n);
}
}
dp[s] = ans;
return dp[s];
}
int main()
{
while(scanf("%d",&n)!=EOF,n)
{
scanf("%d",&m);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++) graph[i][j]=INF;
}
for(int i=; i<=m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
graph[a][b]=graph[b][a] = c;
}
dijkstra(n,);
memset(dp,,sizeof(dp));
dp[]=;
printf("%d\n",dfs(,n));
}
}
 

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. Android toolbar menu 字体点击样式

    今天在做toolbar的时候,右边的菜单的点击事件,就是文字,然后文字的样式,文字的大小,文字的颜色,高了半天.最后发现,文字点下去之后是有样式的,也就是按下去有阴影. 哥哥的耐心好,就知道这不是问题 ...

  2. easyui 获取树的平级根节点的父节点&选择性展示树的一个根节点

    1.easyui的树的根节点一般是几个平级的,怎样获取这些父节点的id? 可以将获取到的平级根节点放在一个数组中 var roots=[]; roots=$("#tree1").t ...

  3. 【HTML&CSS】 第二章:标准模式下的页面与怪异模式下的页面区别

    盒模型 前面提到,盒模型(box mode)是浏览器 Quirks Mode 和 Standards Mode 的主要区别. 描述 对于“盒模型”一词并没有明确的文档定义,它是开发人员描述 CSS 中 ...

  4. 《Cracking the Coding Interview》——第4章:树和图——题目4

    2014-03-19 03:40 题目:给定一棵二叉树,把每一层的节点串成一个链表,最终返回一个链表数组. 解法:前序遍历,遍历的同时向各个链表里添加节点.水平遍历好像还不如前序遍历来得方便. 代码: ...

  5. Scrapy爬取到的中文数据乱码问题处理

    Scrapy爬取到中文数据默认是 Unicode编码的,于是显示是这样的: "country": ["\u56fd\u4ea7\u6c7d\u8f66\u6807\u5f ...

  6. Leetcode 659.分割数组为连续子序列

    分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [1,2,3 ...

  7. LeetCode(一)

    Q&A ONE Given an array of integers, return indices of the two numbers such that they add up to a ...

  8. 【bzoj2618】[Cqoi2006]凸多边形 半平面交

    题目描述 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. 输入 第一行有一个整数n,表示凸多边形的个数,以下依次描述各个多边形.第 ...

  9. SPOJ - BALNUM Balanced Numbers

    题意: 求出所给范围内满足其数位上的奇数出现偶数次,数位上的偶数出现奇数次(或不出现)的数的个数. 思路: 对于0 ~ 9 每个数有3种情况. 1.没出现过 2.出现奇数次 3.出现偶数次 那么就可以 ...

  10. Hibernate中多对多的annotation的写法(中间表可以有多个字段)

    2011-07-04 6:52 一般情况下,多对多的关联关系是需要中间表的: 情况一:如果中间表仅仅是做关联用的,它里面仅有2个外键做联合主键,则使用ManyToMany(不用写中间表的Model,只 ...