HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
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
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.
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
// 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 (记忆化搜索 最短路)的更多相关文章
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 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 ...
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
- HDU 1142 A Walk Through the Forest(Dijkstra+记忆化搜索)
题意:看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A和B 是相连的,当前在 A 处, 如果 A 到终点的最短距离大于 B 到终点的最短距离,则可以从 A 通往 B 处,问满 ...
- 题解报告:hdu 1142 A Walk Through the Forest
题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...
- 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 ...
- hdu 1078 FatMouse and Cheese(简单记忆化搜索)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 一道 ...
- hdu 4753 Fishhead’s Little Game 博弈论+记忆化搜索
思路:状态最多有2^12,采用记忆化搜索!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm& ...
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
随机推荐
- 先登陆面试再者Tabs标签导航,多次网络请求共享cookie,本地存储cookie
1,index.ng.html: <head> <title>ionic todo example</title> </head> <body n ...
- randomize_va_space
proc/sys/kernel/randomize_va_space用于控制Linux下 内存地址随机化机制(address space layout randomization),有以下三种情况 0 ...
- iOS学习之UI可视化编程-XIB
一.Interface Builder可视化编程 1.Interface Builder简介: GUI:图形用户界面(Graphical User Interface,简称GUI,又称图形用户接口)是 ...
- extjs实现多国语音切换
http://kuyur.info/blog/archives/2490 http://blog.chinaunix.net/uid-28661623-id-3779637.html http://b ...
- [转]ubuntu 下minicom超级终端的使用方法
[转]ubuntu 下minicom超级终端的使用方法 http://blog.chinaunix.net/uid-25909619-id-3184639.html 系统环境: Ubuntu 11.0 ...
- LoadRunner - 实战,转发
最近几天一直在读代震军的博客,他是Discuz!NT的设计者,读了他的一系列关于 Discuz!NT的架构设计文章,大呼过瘾,特别是Discuz!NT在解决高访问高并发时所设计的一系列方案,本人尤其感 ...
- MDI窗体 的再度思考
早在敲学生管理系统的时候,青山师哥就跟我说过 MDI 窗体 跟 子窗体的事情,当时只是简单的查询.小小的用了一下,没有太在意. 当再次面对着这个东西更深层的时候才想起师哥那句话.“好好研究一下这个东 ...
- java中的匿名内部类总结(转)
源出处: java中的匿名内部类总结 匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接 ...
- VMWare EXSi 添加新磁盘时 报错 HostStorageSystem.ComputeDiskPartitionInfo 的处理
给 VMWare EXSi 添加新磁盘时报错 : Call "HostStorageSystem.ComputeDiskPartitionInfo" for object &quo ...
- JavaScript 闭包详解
一.Javascript闭包的用途 事实上,通过使用闭包,我们可以做很多事情.比如模拟面向对象的代码风格:更优雅,更简洁的表达出代码:在某些方面提升代码的执行效率. 1.匿名自执行函数 我们知道所有的 ...