A Walk Through the Forest

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

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
 
Source
 
题意:
从起点1到终点2,求这样的路的条数:如果从a点到2的距离大于从b点到2的距离,并且a能到b那么就从a走到b。问这样的路有几条。
代码:
 //并不是求最短路的条数。先dijk出2到每个点的最短路,在记忆化搜索出路径数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAX=;
int mp[][],dis[],vis[],path[];
int n,m;
void dijk()
{
for(int i=;i<=n;i++)
{
dis[i]=mp[][i];
vis[i]=;
}
vis[]=;
for(int i=;i<=n;i++)
{
int Min=MAX,sta=; //sta初始化一下,防止下面找不到j,而出现vis[sta]数组越界。
for(int j=;j<=n;j++)
{
if(!vis[j]&&dis[j]<Min)
{
Min=dis[j];
sta=j;
}
}
vis[sta]=;
for(int j=;j<=n;j++)
{
if(!vis[j]&&mp[sta][j]!=MAX&&dis[j]>dis[sta]+mp[sta][j])
dis[j]=dis[sta]+mp[sta][j];
}
}
}
int dfs(int x)
{
if(path[x]!=-) //记忆化
return path[x];
if(x==)
return ;
path[x]=;
for(int i=;i<=n;i++)
{
if(mp[x][i]!=MAX&&dis[x]>dis[i])
path[x]+=dfs(i);
}
return path[x];
}
int main()
{
int a,b,c;
while(scanf("%d",&n)&&n)
{
scanf("%d",&m);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
mp[i][j]=i==j?:MAX;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
mp[a][b]=mp[b][a]=min(mp[a][b],c);
}
dijk();
memset(path,-,sizeof(path));
int ans=dfs();
printf("%d\n",ans);
}
return ;
}
代码:
 

*HDU1142 最短路+记忆化dfs的更多相关文章

  1. hdu 1142 最短路+记忆化

    最短路+记忆化搜索HDU 1142 A Walk Through the Forest链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 > 题意 ...

  2. 从DFS到记忆化DFS到动态规划

    什么是动态规划? 动态规划(Dynamic Programming)是通过组合子问题的解来解决问题的.动态规划是用于求解包含重叠子问题的最优化问题的方法.其基本思想是,将原问题分解为相似的子问题.在求 ...

  3. 【poj3252】 Round Numbers (数位DP+记忆化DFS)

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...

  4. HDU1978How Many Ways 记忆化dfs+dp

    /*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...

  5. 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919

    大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...

  6. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  7. 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 ...

  8. HDU1142 (Dijkstra+记忆化搜索)

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

  9. 洛谷 P3953 逛公园【spfa+记忆化dfs+bfs】

    spfa预处理出最短路数组dis,然后反向建边bfs出ok[u]表示u能到n点 然后发现有0环的话时候有inf解的,先dfs找0环判断即可 然后dfs,设状态f[u][v]为到u点,还可以跑最短路+v ...

随机推荐

  1. tyvj1191 迎春舞会之三人组舞

    背景     HNSDFZ的同学们为了庆祝春节,准备排练一场舞 描述     n个人选出3*m人,排成m组,每组3人.    站的队形——较矮的2个人站两侧,最高的站中间.    从对称学角度来欣赏, ...

  2. nyoj 106背包问题(贪心专题)

    背包问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w< ...

  3. UI第十五节——UIWebView

    - (void)viewDidLoad {    [super viewDidLoad];    UIWebView *webView = [[UIWebView alloc] initWithFra ...

  4. 浅谈JS之AJAX

    0x00:什么是Ajax? Ajax是Asynchronous Javascript And Xml 的缩写(异步javascript及xml),Ajax是使用javascript在浏览器后台操作HT ...

  5. (转)android自定义组合控件

    原文地址:http://mypyg.iteye.com/blog/968646 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 1.控件布局:以Linea ...

  6. nginx实现请求转发

    反向代理适用于很多场合,负载均衡是最普遍的用法. nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理. nginx 反向代理官方文档: NGINX REVERSE PROXY 当在 ...

  7. 群体结构图形三剑客——PCA图

    重测序便宜了,群体的测序和分析也多了起来.群体结构分析,是重测序最常见的分析内容.群体结构分析应用十分广泛,首先其本身是群体进化关系分析里面最基础的分析内容,其次在进行GWAS分析的时候,本身也需要使 ...

  8. laravel 输出sql

    $queries = DB::getQueryLog(); $a = end($queries); $tmp = str_replace('?', '"'.'%s'.'"', $a ...

  9. js对文本框特殊字符串过滤

    HTML 中 onkeyup="stripscript(this)"; function stripscript(obj) { var s = $(obj).val(); var ...

  10. AT常见问题

    https://m.douban.com/note/247040789/?from=author