A Walk Through the Forest

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

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到B的一条路,可以走的条件

是B到2(家)的路程必须小于从A到2(家)的路程,其实就是A到家的最短路径必须大于B到家的最短路径。

默认一定可以走到家,也就是一定会有一种路线。最后是让你计算有几种路线。可以用迪杰斯特拉算法找一下所有点到终点的最短路,然后DFS搜一下就行了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
int n;//十字路口数
int w[][];
int dist[],dp[];
int s[];
void dijkstra(int v)//迪杰斯特拉算法
{
int i,j,mins,index;
for(i=; i<=n; i++)
{
dist[i] = w[i][v];
s[i] = ;
}
dist[v] = ;
s[v] = ;
for(i=; i<n; i++)
{
mins = ;
for(j=; j<=n; j++)
{
if(s[j]== && dist[j]<mins)
{
mins = dist[j];
index = j;
}
}
if(mins == )//注意 若没有 会在中间溢出
break;
s[index] = ;
for(j=; j<=n; j++)
{
if(s[j]== && dist[j]>dist[index]+w[j][index])
dist[j] = dist[index]+w[j][index];
}
}
} int dfs(int v)//记忆法深搜
{
if(dp[v] != -)
return dp[v];
if(v == )
return ;
int i,temp,sum=;
for(i=; i<=n; i++)
{
if(w[v][i]!= && dist[v] > dist[i])//有路相通而且要去的i点到终点站的距离要比v到终点站的距离小
{
temp = dfs(i);
sum += temp;
}
}
dp[v] = sum;
return sum;
} int main()
{
while(cin>>n && n)
{
int i,j,d,m;
cin>>m;
for(i=; i<=n; i++)
{
dp[i] = -;
for(j=; j<=n; j++)
w[i][j] = ;
}
while(m--)
{
scanf("%d%d%d",&i,&j,&d);
w[i][j] = w[j][i] = d;//无向图
}
//求出各点到终点站的最短距离
dijkstra();//2为终点站
dfs();//从1出发
cout<<dp[]<<endl;
}
return ;
}

HDU1142 A Walk Through the Forest(dijkstra)的更多相关文章

  1. HDU1142 A Walk Through the Forest(最短路+DAG)

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

  2. UVA-10917 Walk Through the Forest (dijkstra+DP)

    题目大意:n个点,m条边的无向图.一个人从起点到终点按照下面的走法:从A走向B当A到终点的最小距离比B到终点的最小距离大时.问从起点到终点有多少路径方案. 题目分析:先用dijkstra预处理出终点到 ...

  3. UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)

    用新模板阿姨了一天,换成原来的一遍就ac了= = 题意很重要..最关键的一句话是说:若走A->B这条边,必然是d[B]<d[A],d[]数组保存的是各点到终点的最短路. 所以先做dij,由 ...

  4. 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

    迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...

  5. 最短路径之迪杰斯特拉(Dijkstra)算法

    迪杰斯特拉(Dijkstra)算法主要是针对没有负值的有向图,求解其中的单一起点到其他顶点的最短路径算法.本文主要总结迪杰斯特拉(Dijkstra)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...

  6. 理解最短路径——迪杰斯特拉(dijkstra)算法

    原址地址:http://ibupu.link/?id=29 1.       迪杰斯特拉算法简介 迪杰斯特拉(dijkstra)算法是典型的用来解决最短路径的算法,也是很多教程中的范例,由荷兰计算机科 ...

  7. 图论——迪杰斯特拉算法(Dijkstra)实现,leetcode

    迪杰斯特拉算法(Dijkstra):求一点到另外一点的最短距离 两种实现方法: 邻接矩阵,时间复杂度O(n^2) 邻接表+优先队列,时间复杂度O(mlogn)(适用于稀疏图) (n:图的节点数,m:图 ...

  8. 算法-迪杰斯特拉算法(dijkstra)-最短路径

    迪杰斯特拉算法(dijkstra)-最短路径 简介: 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中 ...

  9. 数据结构与算法——迪杰斯特拉(Dijkstra)算法

    tip:这个算法真的很难讲解,有些地方只能意会了,多思考多看几遍还是可以弄懂的. 应用场景-最短路径问题 战争时期,胜利乡有 7 个村庄 (A, B, C, D, E, F, G) ,现在有六个邮差, ...

随机推荐

  1. MySQL主主搭建

    1.在MySQL主从的基础上修改: #master1上 [mysqld] server-id=101 log-bin = mysql-bin auto-increment-increment = 2 ...

  2. JavaScript 知识点之escape()与decodeURI()

    解释:escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串. 语法:escape(string)参数描述string必需.要被转义或编码的字符串. 返回值:已编码的 str ...

  3. java replace方法

    一:前言 replace自己老是忘记参数是那个替换那个,自己就把replace方法全部给弄了一遍 二:内容 package org.replaceDemo; public class ReplaceD ...

  4. [洛谷P2420] 让我们异或吧

    题目链接:让我们异或吧 题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是 ...

  5. 【hdu1251-统计难题】Trie

    http://acm.hust.edu.cn/vjudge/problem/16379 题意:给定多个单词,多次询问符合某前缀的单词有多少个. 题解:tire.数组开了5*10^6才A,不然就RE. ...

  6. 文本区 JTextArea 的使用

    文本区JTextArea是对多行文本进行编辑的组件,用空字符来控制文本的格式.eg:"\n"为换行,"\t"为插入一个Tab字符. 文本去JTextArea的常 ...

  7. 在SDK中使用Ubuntu仿真器

    Ubuntu仿真器是开发过程中非常有用,尤其是在没有任何Ubuntu设备时.在将仿真器附加到SDK后,你便可以在上面运行程序,安装点击数据包,等等,类似在物理设备上的操作一样. 在这里,您可以了解如何 ...

  8. python-列表 字典 集合 元祖 字符串的相关总结练习

    1.执行python脚本的两种方式指定解释器执行在交互器中执行 2.简述位.字节的关系:ASCII1个二进制位是计算机里的最小表示单元1个字节是计算机里最小的储存单元二进制位=8bits(位)8bit ...

  9. MySQL 进阶(待发布)

    视图 存储过程 触发器 基本函数

  10. RabbitMQ消息队列(五): 主题分发

    1. 主题(Topics): fanout模式只能进行简单的广播,direct模式虽然在过滤上进行了一定的提升,但是不能支持复杂的条件, 比如我们的日志消息,现在不仅要知道消息级别,也要知道消息来源. ...