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. 关于使用git和github的一点点感想

    第二篇博客 首先附上我的第一个java程序github地址: https://github.com/KingsC123456/FirstJavaHello 其次是关于我的github介绍,因为一直使用 ...

  2. PyTorch教程之Neural Networks

    我们可以通过torch.nn package构建神经网络. 现在我们已经了解了autograd,nn基于autograd来定义模型并对他们有所区分. 一个 nn.Module模块由如下部分构成:若干层 ...

  3. 日期小demo

    有个项目需求是做个在日期上选择的,就是这种: 网上看了几个日期的demo都太厚重了,移植起来太麻烦,然后打算自己写. 就先写个简化的demo看看,主要有几个关键点: 首先要根据当前日期获取这个月有几天 ...

  4. 分享基于分布式Http长连接框架--代码模型

    好的代码应该是方便客户端使用,代码能够自描述,规范化,大众标准化. 而且我相信代码也是有生命的,需要不断的维护它,你以什么样的态度对待它,它就会以同样的态度回敬你,所以在写代码前,先摆好自己的态度(一 ...

  5. bzoj3713 [PA2014]Iloczyn|暴力(模拟)

    斐波那契数列的定义为:k=0或1时,F[k]=k:k>1时,F[k]=F[k-1]+F[k-2].数列的开头几项为0,1,1,2,3,5,8,13,21,34,55,-你的任务是判断给定的数字能 ...

  6. webservice部署到服务器报错

    System.Net.WebException: 基础连接已经关闭: 发送时发生错误. ---> System.IO.IOException: 从传输流收到意外的 EOF 或 0 个字节. 在 ...

  7. M-自适应宽高样式

    1 绝对定位 position: absolute; top: 0px; bottom: 0px; left: 0px; width: 100%; overflow: hidden;

  8. Visual Studio 自定义项目模板

    经常我们需要新建一个项目,然后新建我们的View文件夹,ViewModel文件夹,Model文件夹,还有把我们的ViewModelBase放入我们的VIewModel,如果还用框架,还需要加上好多. ...

  9. mb_substr函数

    定义和用法 mb_substr() 截取字符串中指定长度字符 注:常用于中文截取,可以避免截取时候出现乱码,即截取半个字符的情况. 类似函数 substr(),iconv_substr() 语法 mb ...

  10. JQuery-基础学习1

    1)JQuery语法 jquery语法是为HTML元素的选取编制,可以对元素执行某些操作. 基础语法是:$(selector).action() 美元符号定义JQuery 选择符(selector)& ...