A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 118 Accepted Submission(s): 57
 
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
University of Waterloo Local Contest 2005.09.24
 
Recommend
Eddy
/*
最短路的条数
*/
#include<bits/stdc++.h>
#define N 1010
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int mapn[N][N];
int vis[N];
int dis[N];
int dp[N];//表示到点i的最短路有多少条
int x,y,val;
/*
求出到各点的最短路
*/
void dijkstra(int s){
int i,j,minn,pos;
memset(vis,,sizeof(vis));
for(i = ; i<=n; i++)
dis[i] = mapn[s][i];
dis[s]=;
vis[s]=;
for(i=;i<=n;i++){
minn=INF;
for(j=;j<=n;j++){
if(dis[j]<minn&&!vis[j]){
minn=dis[pos=j];
}
}
/*
如果不加这句话minn没有找到的话就是INF就会越界的
*/
if(minn==INF) break;
vis[pos]=;
for(j=;j<=n;j++){
if(!vis[j]&&dis[pos]+mapn[pos][j]<dis[j]){
dis[j]=dis[pos]+mapn[pos][j];
}
}
}
}
/*
然后记忆化深搜出结果
*/
int dfs(int v){
int sum=;
if(dp[v]!=-) return dp[v];//搜到第v个结点的时候直接拿过来用就行了
if(v==) return ;
for(int i=;i<=n;i++){
/*
状态转移主要在于dis[v]
*/
if(mapn[v][i]!=INF&&dis[v]>dis[i])
sum+=dfs(i);
}
dp[v]=sum;
return dp[v];
}
int main(){
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<=n;i++){
dp[i]=-;
for(int j=;j<=n;j++){
mapn[i][j]=INF;
}
}
scanf("%d",&m);
while(m--){
scanf("%d%d%d",&x,&y,&val);
mapn[x][y]=mapn[y][x]=val;
}
/*将每点到2的最短距离打到dis数组中去*/
dijkstra();
printf("%d\n",dfs());
}
return ;
}
/*
36 0 37 34 24
2
4 0 3 3 1 1 2
4
*/

A Walk Through the Forest的更多相关文章

  1. A Walk Through the Forest[HDU1142]

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

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

  5. UVa 10917 A Walk Through the Forest

    A Walk Through the Forest Time Limit:1000MS  Memory Limit:65536K Total Submit:48 Accepted:15 Descrip ...

  6. hdu_A Walk Through the Forest ——迪杰特斯拉+dfs

    A Walk Through the Forest Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/ ...

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

  8. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

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

随机推荐

  1. Android忽略文件以及.gitignore规则不生效的可行解决方案

    github官方的忽略规则:https://github.com/github/gitignore/blob/master/Android.gitignore 我司项目中的忽略规则: *.iml .g ...

  2. jenkins~集群分发功能的具体实现

    前一讲主要说了jenkins分发的好处<jenkins~集群分发功能和职责处理>,它可以让具体的节点干自己具体的事,比如windows环境下的节点,它只负责编译,发布windows的生态环 ...

  3. 如何从两个List中筛选出相同的值

    问题 现有社保卡和身份证若干,想要匹配筛选出一一对应的社保卡和身份证. 转换为List socialList,和List idList,从二者中找出匹配的社保卡. 模型 创建社保卡类 /** * @a ...

  4. Rundeck部署和基本使用

    rundeck 介绍 Rundeck 是一款能在数据中心或云环境中的日常业务中使程序自动化的开源软件.Rundeck 提供了大量功能,可以减轻耗时繁重的体力劳动.团队可以相互协作,分享如何过程自动化, ...

  5. NOIP2017SummerTraining0710

    个人感受:这套题,题目泄露,没什么好打的,第一题刚开始题目理解错误,后来还行,第二道题,打了一个50还是60分的dp,第三道暴力过了小数据,拿了200分,排名15+. 问题 A: 七天使的通讯 时间限 ...

  6. Distribute Candies

    Given an integer array with even length, where different numbers in this array represent different k ...

  7. 简单又炫酷的two.js 二维动画教程

      前  言 S     N 今天呢给大家介绍一个小js框架,Two.JS.其实在自己学习的过程中并没有找到合适的教程,所以我这种学习延迟的同学是有一定难度的,然后准备给大家整理一份,简单易懂的小教程 ...

  8. fitnesse - 一个简单的例子(slim)

    fitnesse - 一个简单的例子(slim) 2017-09-30 目录1 编写测试代码(Fixture code)2 编写wiki page并运行  2.1 新建wikiPage  2.2 运行 ...

  9. git reflog -- 显示所有提交

    格式:           git commit  [选项] <path> 选项 git commit -a 提交所有改动的文件(a -- all) git commit -m 提交说明( ...

  10. Druid源码阅读之连接池

    概述 Druid是阿里巴巴开源的一个数据库连接池 源码地址.下面简单分析一下连接池是怎么实现的 怎么开始阅读 如果使用过Druid连接池的都只要在Spring配置中配置jdbc的时候配置Driver是 ...