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. Python习题之列表排序,4种方法

    def sort_list_method_1(a): return sorted(a) print(sort_list_method_1([1, 4, 2])) def sort_list_metho ...

  2. Leetcode题目practice

    目录 Leetcode题目解答 1. 删除最外层的括号 2. 两数之和 3. 宝石与石头 4. 移除元素 5.删除排序数组中的重复项 6.寻找两个有序数组的中位数 7.盛最多水的容器 8.存在重复元素 ...

  3. 洛谷 2777 [AHOI2016初中组]自行车比赛

    [题解] 为了让某个选手能够获得总分第一,就让他最后一天的得分是n,并且让别的选手的得分的最大值尽量小.于是我们先把目前积分排序,并且让他们最后一天的排名刚好与积分排名相反.即某个积分排名为X的人最后 ...

  4. BZOJ 4415 洛谷 3988 [Shoi2013]发牌

    [题解] 权值线段树.查询当前牌堆顶的牌并且删掉就好了. #include<cstdio> #include<algorithm> #define N 3000010 #def ...

  5. Spring MVC 概述

    [简介] Spring MVC也叫Spring web mvc,属于表现层的框架.SpringMVC是Spring框架的一部分,是在Spring 3.0后发布的. 由以上Spring的结构图可以看出, ...

  6. 对jetbrains全系列可用例:IDEA、WebStorm、phpstorm、clion等----https://blog.csdn.net/u014044812/article/details/78727496

    https://blog.csdn.net/u014044812/article/details/78727496 pyCharm最新2018激活码

  7. HDU 4903 (模拟+贪心)

    Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a he ...

  8. 从尾到头打印链表——剑指Offer

    https://www.nowcoder.net/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=11156&tPage= ...

  9. C++ - 模板函数须要类型转换时使用友元(friend)模板函数

    模板函数须要类型转换时使用友元(friend)模板函数 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24357301 非模板函数 ...

  10. 1.7-BGP④

    注意:默认路由ip route 0.0.0.0 0.0.0.0 12.1.1.1是不可以作为BGP邻居TCP始发连接的(但回包可以) 要配静态路由:ip route 13.1.1.3 255.255. ...