A Walk Through the Forest

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

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
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  2722 2962 2923 1116 2433 
 

开始题意理解错了,以为求最短路径的数目。其实求的是在点1到点2的路径中,经过的路段Vij要求d[i]>d[j](d[i]为点i到到点2的最短路),求满足要求的路径数。

最短路径:

先求出点2到其他店的最短路,然后记忆化搜索得解。

 //31MS     820K    1533B     G++
#include<iostream>
#include<vector>
#include<queue>
#define N 1005
#define inf 0x7fffffff
using namespace std;
struct node{
int v,d;
node(int a,int b){
v=a;d=b;
}
};
vector<node>V[N];
int vis[N],d[N];
int v;
int n,m;
int ans[N];
void dij(int s)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
d[i]=inf;
d[s]=;
queue<int>Q;
Q.push(s);
vis[s]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
int m=V[u].size();
for(int i=;i<m;i++){
int v=V[u][i].v;
int w=V[u][i].d;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
int dfs(int u)
{
if(u==) return ;
if(ans[u]!=) return ans[u];
int m=V[u].size();
int cnt=;
for(int i=;i<m;i++){
int v=V[u][i].v;
int w=V[u][i].d;
if(d[v]<d[u])
cnt+=dfs(v);
}
return ans[u]=cnt;
}
int main(void)
{
int a,b,c;
while(scanf("%d",&n),n)
{
scanf("%d",&m);
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++) V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
V[a].push_back(node(b,c));
V[b].push_back(node(a,c));
}
dij();
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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

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

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

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

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

  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(SPFA+记忆化搜索DFS)

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

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

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

随机推荐

  1. 【NAS】CIFS用户场景需求分析

    1.everyone用户 1.1: 场景描述:共享目录为rule,所有用户都可以查看,但是不能修改: 解决方法:在smb.conf里配置read only = yes,具体示例如下: [rule] p ...

  2. P1126 机器人搬重物

    P1126 机器人搬重物 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有 ...

  3. JDBC 工具类模板c3p0

    JDBC 工具类模板 package com.itheima.sh.utils; import com.mchange.v2.c3p0.ComboPooledDataSource; import ja ...

  4. MySQL 取得字段子串修改

    MySQL 中, GeneID 为 GRMZM2G549533_P01,1123,45 , 需要修改为 GRMZM2G549533_P01 update test set GeneID=SUBSTRI ...

  5. MAC 更新brew 镜像源

    mac 更新brew镜像源 cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url o ...

  6. 容器云技术:容器化微服务,Istio占C位出道

    在精彩的软件容器世界中,当新项目涌现并解决你认为早已解决的问题时,这感觉就像地面在你的脚下不断地移动.在许多情况下,这些问题很久以前被解决,但现在的云原生架构正在推动着更大规模的应用程序部署,这就需要 ...

  7. lesson 15 Fifty pence worth of trouble

    lesson 15 Fifty pence worth of trouble appreciate =be grateful for We really appreciate all the help ...

  8. C if 判断 else 否则

    #include <stdio.h> int main(int argc, char **argv) { //新建三个变量进行比较 int a,b,c; //输入三个变量的值scanf(& ...

  9. 使用getid3获取音频文件信息

    今天有个需求,在上传音频文件时候自动获取音频的秒数,和大家分享一下. 首先把getid3的包下载下来 链接:https://pan.baidu.com/s/1Qmdj-I4boz9Sm9GFsON0D ...

  10. Eclipse上安装Activiti插件

    今天我们来讲下如何在Eclipse上安装Activiti插件,以后我们要用这个插件来画流程设计图: 这个插件名字是:Activiti BPMN 2.0 designer 具体使用,可以参考官方用户指南 ...