A Walk Through the Forest

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

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
 
解题思路:这题与HDU的《校园漫步》差不多,也是先将每点到终点的最短距离保存起来,然后记忆化搜索!
 
解题代码:

 // File Name: A Walk Through the Forest 1142.cpp
// Author: sheng
// Created Time: 2013年07月18日 星期四 19时57分53秒 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; #ifdef WINDOWS
#define LL __int64
#define LLD "%I64d"
#else
#define LL long long
#define LLD "%lld"
#endif const int max_n = ;
const LL INF = 0x3fffffff;
int n, m;
struct point
{
int x, y;
}; int map[max_n][max_n];
LL vis[max_n], dis[max_n];
LL s[max_n]; LL DFS(int p)
{
s[] = ;
if (s[p] > )
return s[p]; //记忆化
for (int i = ; i <= n; i ++)
{
if ((dis[i] < dis[p]) && map[p][i] < INF)
s[p] += DFS(i); }
return s[p];
} int main ()
{
while (~scanf ("%d", &n) && n)
{
scanf ("%d", &m);
for (int i = ; i <= n; i ++)
for (int j = ; j <= n; j ++)
map[i][j] = INF;
for (int i = ; i < m; i++)
{
int a, b, c;
scanf ("%d%d%d", &a, &b, &c);
map[a][b] = map[b][a] = c;
}
memset(vis, , sizeof (vis));
memset(s, , sizeof(s));
for (int i = ; i <= n; i ++)
{
dis[i] = map[][i];
}
vis[] = ;
for (int i = ; i < n; i ++)
{
int k;
LL min = INF;
for (int j = ; j <= n; j ++)
{
if (!vis[j] && min > dis[j])
{
min = dis[j];
k = j;
}
}
vis[k] = ;
for (int j = ; j <= n; j ++)
{
if (!vis[j] && dis[j] > dis[k] + map[k][j])
dis[j] = dis[k] + map[k][j];
}
}
dis[] = ; //因为从2到2是距离为0的
DFS();
printf (LLD"\n", s[]);
}
return ;
}

HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)的更多相关文章

  1. hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  2. HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  3. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  4. HDU 1142 A Walk Through the Forest(Dijkstra+记忆化搜索)

    题意:看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A和B 是相连的,当前在 A 处, 如果 A 到终点的最短距离大于 B 到终点的最短距离,则可以从 A 通往 B 处,问满 ...

  5. 题解报告:hdu 1142 A Walk Through the Forest

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...

  6. HDU - 5001 Walk(概率dp+记忆化搜索)

    Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started t ...

  7. hdu 1078 FatMouse and Cheese(简单记忆化搜索)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 一道 ...

  8. hdu 4753 Fishhead’s Little Game 博弈论+记忆化搜索

    思路:状态最多有2^12,采用记忆化搜索!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm& ...

  9. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

随机推荐

  1. hdu 4613 Points<计算几何>

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4613 题意: 判断一个集合中的点能不能由另一个集合中的点,通过平移,旋转,放缩得到~ 思路:先求出集合中的 ...

  2. Linux分区

    硬盘分区主要分为基本分区和扩展分区两种,基本分区和扩展分区的数目之和不能大于四个.且基本分区可以马上被使用但不能再分区.扩展分区必须再进行分区后才能进行使用,也就是说它必须进行二次分区.扩展分区再分下 ...

  3. struct的成员对齐问题-结构体实际大小问题

    struct的成员对齐 注意:为了方便说明,等号左边是每个数据单独所占长度,右边是最终空间大小,以字节为单位. 一.什么时间存在对其问题:(32位机对齐方式是按照4字节对其的,以下所有试验都是在32位 ...

  4. Resource is out of sync with the file system

    Resource is out of sync with the file system解决办法: 在eclipse或mycelipse中,启动run on server时或查看项目文件时报错:Res ...

  5. Oracle Rac crs无法启动

    OS:ORACLE LINUX 5.7 DB:11.2.0.3 RAC:YES 故障:1.两节点RAC,节点分别为linuxdb1.linuxdb2,其中节点linuxdb2服务器出现故障,无法启动2 ...

  6. 我爱我家:我为什么选择AppCan?

    10年前,说起手机,大家联想到的词大概是:电话.短信.QQ.拍照,以及贪吃蛇等有限的几个小游戏.而如今,手机毫无疑问已经成为人们生活中不可或缺的部分.这是一个神奇的东西:通讯工具,外卖神器,游戏机,移 ...

  7. java 版本安装

    系统:Ubuntu 10.04 JDK:jdk-6u20-linux-i586.bin 当然,我已经在sun的官方网站上下载好了必要的jdk,由于是在linux下安装,跟平时习惯的windows有所不 ...

  8. Linux虚拟机配置本地yum源

    刚开始使用Linux,自己构建了一个Linux虚拟机之后,在使用yum install的时候,经常是出错,提示连接不上. 一直以为是自己构建的虚拟机的问题,后来在网上查找了一些资料,才发现:需要配置本 ...

  9. Android实现SQLite数据库联系人列表

    Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...

  10. C# 把引用的dll嵌入到exe文件中

    当发布的程序有引用其它dll, 又只想发布一个exe时就需要把dll打包到exe 当然有多种方法可以打包, 比如微软的ILMerge,混淆器附带的打包... 用代码打包的实现方式也有很好,本文只是其中 ...