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
解题思路:从起点1到终点2,如果从a点到2的距离大于从b点到2的距离,并且a能到b(即两点间有边(路)),那么就从a走到b。问这样的路有几条。Dijkstra+记忆化搜索!
AC代码:
 #include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int n,m,a,b,c,dis[MAXN],pis[MAXN],G[MAXN][MAXN];
bool vis[MAXN];
void Dijkstra()
{
for(int i=;i<=n;i++)//把终点2当成起点,求出起点2到各节点的最短路径
dis[i]=G[][i];
dis[]=;vis[]=true;
for(int i=;i<n;i++){
int k=-;
for(int j=;j<=n;j++)
if(!vis[j] && (k==-||dis[j]<dis[k]))k=j;
if(k==-)break;
vis[k]=true;
for(int j=;j<=n;j++)
if(!vis[j])dis[j]=min(dis[j],dis[k]+G[k][j]);
}
}
int dfs(int x){//记忆化搜索
if(pis[x]!=-)return pis[x];
if(x==)return ;
pis[x]=;
for(int i=;i<=n;++i)//x到i有路且x到终点距离大于i到终点距离
if(G[x][i]!=INF && dis[x]>dis[i])pis[x]+=dfs(i);
return pis[x];
}
int main()
{
while(~scanf("%d",&n) && n){
scanf("%d",&m);
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
G[i][j]=(i==j?:INF);
memset(vis,false,sizeof(vis));
memset(pis,-,sizeof(pis));
for(int i=;i<=m;++i){
scanf("%d %d %d",&a,&b,&c);
G[a][b]=G[b][a]=c;
}
Dijkstra();
printf("%d\n",dfs());
}
return ;
}
 

题解报告:hdu 1142 A Walk Through the Forest的更多相关文章

  1. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

  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(最短路+记忆化搜索)

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

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

  5. HDU 1142 A Walk Through the Forest(最短路+dfs搜索)

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

  6. 【解题报告】HDU -1142 A Walk Through the Forest

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...

  7. hdu 1142 A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...

  8. HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

    题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...

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

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

随机推荐

  1. Python3.0科学计算学习之类

    类: Python中的类是一个抽象的概念,甚至比函数还要抽象.可以把它简单的看作是数据以及由存取.操作这些数据的方法所组成的一个集合.类是Python的核心概念,是面向对象编程的基础. 类有如下的优点 ...

  2. linux动态库加载路径修改

    1.在 /etc/ld.so.conf 文件中添加搜索路径,重启或者 ldconfig 生效: 2.在 /etc/ld.so.conf.d 目录下添加 *.conf 文件,其中可以添加搜索路径,重启获 ...

  3. 小数化分数的O(log2n)解法

    具体约束: 给定一个小数x,x满足0<=x<1,且保证给定的x保留了18位小数 输出一个分数,使得分母不超过1e9,分子分母互质,且在满足这些条件的情况下最接近x 了解一下法雷数列和ste ...

  4. getContextPath和getRealPath的区别-----其实主要区别就是相对路径和绝对路径

    getContextPath和getRealPath的区别 其实主要区别就是相对路径和绝对路径 https://blog.csdn.net/zsmj_2011/article/details/4121 ...

  5. Codeforces 263A. Appleman and Easy Task

    A. Appleman and Easy Task time limit per test  1 second memory limit per test  256 megabytes input  ...

  6. sheepdog简介

    1.corosync,single ring最多支持50个节点:zookeeper,500个节点可稳定支撑,1000-1500个节点挑战比较大,需要优化消息传递机制. 2.sheepdog一开始为分布 ...

  7. C# 解决EXCEL单元格合并,行高无法自适应问题

    解决方法:根据单元格内容长度,设置单元格所在行的行高 public static float getExcelCellAutoHeight(string strtest, float fontCoun ...

  8. 二叉堆练习3&【模板】堆

    时间限制: 3 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 Inp ...

  9. 五语言学习系列 C,C++,Objective-C,Java,C# (一)历史

    C:由AT&T贝尔实验室的Dennis Ritchie于1972年创建的,是专为开发者设计的语言. C++:在C基础上,1983年又由贝尔实验室的Bjarne Strou-strup推出了C+ ...

  10. T5090 众数 codevs

    http://codevs.cn/problem/5090/ 时间限制: 1 s  空间限制: 1000 KB  题目等级 : 青铜 Bronze 题目描述 Description 由文件给出N个1到 ...