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. iOS开发-AFNetworking参数和多文件同时上传【多文件上传】

    1. 前言 在项目开发中,我们经常需要上传文件,例如:上传图片,上传各种文件,而有时也需要将参数和多个文件一起上传,不知道大家的项目中遇到了没有,我在最近的项目中,就需要这样的一个功能:同时上传参数. ...

  2. JAVA 一步一步向上爬

    Java分为基本数据类型和引用数据类型(类.接口.数组) Integer.MAX_VALUE 浮点型默认为double java采用Unicode char为两个字节 Unicode为每一个字符定制了 ...

  3. “一切都是消息”--MSF(消息服务框架)入门简介

    “一切都是消息”--这是MSF(消息服务框架)的设计哲学. MSF的名字是 Message Service Framework 的简称,中文名称:消息服务框架,它是PDF.NET框架的一部分. 1,M ...

  4. Win7 32位系统下Sublime text 3的安装以及配置C/C++、java、python的开发环境方法

    本人初学者,此文仅是对这几天鼓捣subime text 3一点微不足道的经验总结,如有明显错误,欢迎指正! 好了,废话少说,进入正题,之前编程java一直用的是eclipse,java的主流IDE,后 ...

  5. 再起航,我的学习笔记之JavaScript设计模式28(委托模式)

    ## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...

  6. thinkphp增删改查

    添加数据: (添加单行数据) // 添加成功返回1,失败返回0 (添加多行数据) // 返回添加数据的条数 删除数据: 修改数据: (修改单个字段) (修改多个字段) // 修改成功返回1,失败返回0 ...

  7. Java面向对象 GUI 补录

     Java面向对象 GUI 补录 知识概要:(1)GUI和CLI                   (2)AWT和SWING                   (3)AWT继承关系图      ...

  8. ubuntu环境下lnmp环境搭建(2)之Nginx

    1. ubuntu编译安装nginx http://www.cnblogs.com/zhangjun516/archive/2013/02/03/2890990.html 1. 手动编译安装 Ngin ...

  9. Linux基本符号

    Linux环境下一些常用的符号 ; 多个命令的分隔符 / 根目录或路径分隔符 > 重定向,数据沿箭头方向流动,原来文件内容会被丢弃 >> 追加重定向,在原来文件结尾追加内容 .. 上 ...

  10. PPT排版细节,写给大家看的设计书,完美总结

    原创作者:陈玓玏 相信每一位小宝贝在工作中都会被老板用PPT虐无数遍,虐到自己怀疑人生.奈何在网上随手一搜,出现的各类招聘要求都躲不开"熟练掌握PPT制作",尤其是各类科技公司.咨 ...