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. 百度富文本编辑器Ueditor使用

    首先我们登上ueditor下载,可以看到多种版本. UBuilder:可以自己选择需要的工具. 我用的开发版,Java的jsp版本,在这里是全部工具,但是工具在配置文件中也是可以自己选择的. 下载下来 ...

  2. mybatis快速入门(五)

    今天写写user表和orders表的mybatis的高级映射,一对一映射和一对多映射 1.创建一个orders.java文件 1.1一对一映射,一条订单对应一个用户 package cn.my.myb ...

  3. 强大的桌面用 PDF 重排工具:K2pdfopt 简明教程

    用 Kindle 阅读 PDF 一直以来都遭到小伙伴们的无限吐槽,在那 Kindle 还能越狱的时代,我们有 Koreader 之类优秀的 Kindle 第三方插件实现 PDF 文档的实时重排,但是随 ...

  4. Excel的实用函数

    在介绍Excel函数前先说明两个概念:公式和函数. 公式:由用户自行设计对工作表进行计算和处理的计算式,以等号"="开始,其内部可以包括函数.引用.运算符和常量. 函数:即是预先定 ...

  5. mysql更新某个字符串字段的部分内容

    如果现在需要Mysql更新字段重部分数据,而不是全部数据,应该采用何种方法呢?下面介绍了两种情况下Mysql更新字段中部分数据的方法,供您参考. Mysql更新字段中部分数据第一种情况: update ...

  6. 从Thread.start()方法看Thread源码,多次start一个线程会怎么样

    这篇文章作为Thread类源码剖析的补充,从一个侧面来看Thread源码.也解答了面试高频问题:"多次start一个线程会怎么样?" 答案是:java.lang.IllegalTh ...

  7. ThinkPHP中:add()和addAll()的区别

    1.add()是记录单条插入 // 添加一条数据 $User = M("User"); // 实例化User对象 $data['name'] = 'ThinkPHP'; $data ...

  8. GCD hdu1695容斥原理

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. POJ1422Air Raid(二分图,最小不相交路径覆盖)

    Air Raid Consider a town where all the streets are one-way and each street leads from one intersecti ...

  10. IFrame父页面和子页面的交互

    现在在页面里面用到iframe的情况越来越少了,但有时还是避免不了,甚至这些页面之间还需要用js来做交互,那么这些页面如何操作彼此的dom呢?下面将会逐步介绍. 1.父页面操作子页面里面的dom 下面 ...