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. 浅谈HAL

    参考:http://blog.csdn.net/mr_raptor/article/details/8074549 代码实现:http://blog.csdn.net/mr_raptor/articl ...

  2. Windows 10 IoT Core Samples

    Windows 10 IoT Core Samples Welcome to the Windows 10 IoT Core Samples These samples have been valid ...

  3. 数据密集型 和 cpu密集型 event loop

    Node.js在官网上是这样定义的:“一个搭建在Chrome JavaScript运行时上的平台,用于构建高速.可伸缩的网络程序.Node.js采用的事件驱动.非阻塞I/O模型使它既轻量又高效,是构建 ...

  4. 微信支付.NET版开发总结(JS API),好多坑,适当精简。

    前2天,做一个手机网页的微信支付的项目,费了好些周折,记录一下.接下来,按照开发步骤,细数一下,我遇到的那些坑. [坑1]官方邮件中下载的demo只有PHP版本,其他版本没有给链接.可能让人误以为只有 ...

  5. Python实现nb(朴素贝叶斯)

    Python实现nb(朴素贝叶斯) 运行环境 Pyhton3 numpy科学计算模块 计算过程 st=>start: 开始 op1=>operation: 读入数据 op2=>ope ...

  6. UIGestureRecongnizer 手势检测 swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  7. 线程同步 Lock接口

    同步:★★★★★ 好处:解决了线程安全问题. 弊端:相对降低性能,因为判断锁需要消耗资源,产生了死锁. 定义同步是有前提的: 1,必须要有两个或者两个以上的线程,才需要同步. 2,多个线程必须保证使用 ...

  8. 【上传AppStore】iOS项目上传到AppStore步骤流程(第一章) - 上传新的app

    1.登录developer.apple.com 2.点击member center后 然后如下图 3.点击certificates Identifiers 进下图 界面基本介绍请看图 : 4. 其次创 ...

  9. JavaScript原型与原型链学习笔记

    一.什么是原型?原型是一个对象,其他对象可以通过它实现属性继承.简单的说就是任何一个对象都可以成为原型 prototype属性: 我们创建的每个函数都有一个prototype属性,这个属性是一个指针, ...

  10. 深入理解jQuery中live与bind方法的区别

    本篇文章主要是对jQuery中live与bind方法的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 注意如果是通过jq添加的层和对象一定要用live(),用其他的都不起作用 ...