A Walk Through the Forest

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

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到家的距离必任意一点从A到家的都小,求他回家路线的方案数
//最短路
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
const int N=;
ll g[N][N],vis[N],n,m,k,x,y,z;
ll dp[N],dis[N];
void init()
{
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
g[i][j]=g[j][i]=INF;
}
g[i][i]=;
}
}
void dij(int x)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
dis[i]=g[x][i];
}
vis[x]=;
int v=x;
int minn;
for(int i=;i<=n;i++)
{
minn=INF;
for(int j=;j<=n;j++)
{
if(!vis[j] && minn>dis[j])
{
v=j;
minn=dis[j];
}
}
vis[v]=;
for(int j=;j<=n;j++)
{
if(!vis[j]) dis[j]=min(dis[j],dis[v]+g[v][j]);
}
}
}
ll dfs(ll now)
{
if(dp[now]>) return dp[now];
if(now==) return ;
for(int i=;i<=n;i++)
{
if(g[now][i]!=INF && dis[now]>dis[i])
dp[now]+=dfs(i);
}
return dp[now];
}
int main()
{
while(scanf("%lld",&n) && n)
{
init();
scanf("%lld",&m);
for(int i=;i<m;i++)
{
scanf("%lld%lld%lld",&x,&y,&z);
g[x][y]=g[y][x]=min(g[x][y],z);
}
dij();
memset(dp,,sizeof(dp));
printf("%lld\n",dfs());
}
return ;
}

HDU 1142 A Walk Through the Forest(最短路+dfs搜索)的更多相关文章

  1. HDU 1142 A Walk Through the Forest(Dijkstra+记忆化搜索)

    题意:看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A和B 是相连的,当前在 A 处, 如果 A 到终点的最短距离大于 B 到终点的最短距离,则可以从 A 通往 B 处,问满 ...

  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 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

  4. hdu 1142 A Walk Through the Forest

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

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

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

  7. 题解报告:hdu 1142 A Walk Through the Forest

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...

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

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

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

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

随机推荐

  1. iOS 动画Animation - 5:UIBezier

    首先说明:这是一系列文章,參考本专题下其它的文章有助于你对本文的理解. 在之前的bolg中大家会发现总是会出现UIBezier,可是我也没有做过多介绍,今天就集中介绍一下UIBezier.首先.UIB ...

  2. easyui combobox keyhandler使用

    easyui combobox keyhandler使用 在combo组件中有属性:   keyHandler : { up : function() { console.log('upupup'); ...

  3. unix mkdir命令的使用方法

    [语法]:   mkdir [-m 模式] [-p]  文件夹名 [说明]:  本命令用于建立文件夹,文件夹的存取模式由掩码(umask)决定,要求对其父文件夹具有写权限,文件夹的UID和GID为实际 ...

  4. mediawiki使用笔记

    https://www.cnblogs.com/ToDoToTry/p/4475067.html

  5. 安装配置FreeBSD9全过程体验

    安装配置FreeBSD9全过程体验(时长11分钟) 总所周知,FreeBSD是一个高效.稳定的UNIX操作系统.在今年年初,FreeBSD 又发布了9.0版本,它将采用全新的文本安装器,升级ZFS文件 ...

  6. SQLite: sqlite_master(转)

    转自:http://blog.sina.com.cn/s/blog_6afeac500100yn9k.html SQLite数据库中一个特殊的名叫 SQLITE_MASTER 上执行一个SELECT查 ...

  7. customErrors 元素(ASP.NET 设置架构)

    1.适用版本:.NET Framework 4 2.元素定义:为 ASP.NET 应用程序提供有关自定义错误消息的信息. 可以在应用程序文件层次结构中的任意级别上定义 customErrors 元素. ...

  8. 第一次接触正则表达式/^[A-Za-z_][A-Za-z0-9_]{5,15}$/

    /^[A-Za-z_][A-Za-z0-9_]{5,15}$/ /^$/ :完整表达式 ^ :表示以什么开始,或者取反 $ :结束 ^[A-Za-z_] : 以字母开始,无论大小都可以: [^A-Za ...

  9. Python学习笔记 capture 1

    最近开始学习Python3.x,真的感觉Python的语法与C++,Java有很大的不同,Python从某些方面来说语法更简单.Python作为一种解释性语言和编译型语言如C++来说,还是各有千秋的. ...

  10. C# Socket服务端及多客户端连接通信实现

    服务端代码(控制台示例): static List<Socket> Sockets = new List<Socket>(); static void Main(string[ ...