A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9081 Accepted Submission(s): 3353 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

学到了DAG中如何记忆化(也可以按距离排序后处理)

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<memory.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
const int maxm=;
int Laxt[maxn];
int len[maxm],To[maxm],Next[maxm],cnt,ans;
int dis[maxn],used[maxn],p[maxn];
queue<int>q;
void _add(int u,int v,int d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
len[cnt]=d;
}
void _dij(){
while(!q.empty()){
int u=q.front();q.pop();
used[u]=;
for(int i=Laxt[u];i;i=Next[i]){
if(dis[To[i]]>dis[u]+len[i]){
dis[To[i]]=dis[u]+len[i];
if(!used[To[i]])
q.push(To[i]);
}
}
}
}
int _find(int v)
{
if(p[v]) return p[v];
int sum=;//使用sum而不是直接p[v]加,保证了加完后才使用,保证了DAG的方向性。
for(int i=Laxt[v];i;i=Next[i])
if(dis[To[i]]<dis[v])
sum+=_find(To[i]);
p[v]=sum;
return p[v];
}
int main()
{
int n,m,i,j,k,x,y,z;
while(~scanf("%d",&n)){
if(n==) return ;
scanf("%d",&m);
cnt=ans=;
memset(dis,0x7F,sizeof(dis));
memset(Laxt,,sizeof(Laxt));
memset(used,,sizeof(used));
memset(p,,sizeof(p));
for(i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
_add(x,y,z);
_add(y,x,z);
}
p[]=;
dis[]=;
used[]=;
q.push();
_dij();
printf("%d\n",_find());
}
return ;
}

HDU1142 A Walk Through the Forest(最短路+DAG)的更多相关文章

  1. HDU1142 A Walk Through the Forest(dijkstra)

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

  2. A Walk Through the Forest (最短路+记忆化搜索)

    Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...

  3. hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)

    看到这道题,想起了我家旁边的山! 那是一座叫做洪山寨的山,据说由当年洪秀全的小妾居住于此而得名! 山上盛产野果(很美味)! 好久没有爬上去了! #include<stdio.h> #inc ...

  4. UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)

    用新模板阿姨了一天,换成原来的一遍就ac了= = 题意很重要..最关键的一句话是说:若走A->B这条边,必然是d[B]<d[A],d[]数组保存的是各点到终点的最短路. 所以先做dij,由 ...

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

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

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

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

  8. A Walk Through the Forest[HDU1142]

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

  9. hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)

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

随机推荐

  1. 分享45个android实例源码,很好很强大.收藏吧!!!

    andriod闹钟源代码 http://www.apkbus.com/android-20974-1-1.html android源码分享之指南针程序 http://www.apkbus.com/an ...

  2. windows系统,优化C盘空间的方法

    C盘在使用过程中,内容会越来越多,剩余空间越来越小.如何清理出更多空间呢?以windows7为例 转载请保留 http://www.cnblogs.com/lion-zheng/ cleanmgr w ...

  3. 验证Oracle收集统计信息参数granularity数据分析的力度

    最近在学习Oracle的统计信息这一块,收集统计信息的方法如下: DBMS_STATS.GATHER_TABLE_STATS ( ownname VARCHAR2, ---所有者名字 tabname ...

  4. Swing-JList用法-入门

    注:本文所有素材均来源于How to Use Lists,本文为该文章的学习笔记. JList是Swing中的列表控件,形状如下所示: 实际上,JList有3种不同的样式: 分别为HORIZONTAL ...

  5. 深度学习(一)cross-entropy softmax overfitting regularization dropout

    一.Cross-entropy 我们理想情况是让神经网络学习更快 假设单模型: 只有一个输入,一个神经元,一个输出   简单模型: 输入为1时, 输出为0 神经网络的学习行为和人脑差的很多, 开始学习 ...

  6. 在linux下通过hexdump生成一个十六进制的文本保存文件,解析此文件转变成正常源代码文件。

    举例说明: 此十六进制保存的文件为此源代码hexdump生成的: #include<stdio.h> #include<string.h> #include<stdlib ...

  7. cxgrid学习

    delphi cxgrid控件哪个属性是设置不能编辑? cxgrid控件cxgridDBTable的OptionsData可以选择操作 cxGrid1DBTableView1下选择cxGrid1DBT ...

  8. C# 泛型集合

    原文出处我的wiki,转载请说明出处 考虑到泛型在集合类型中的广泛应用,这里一起讨论. 1. 泛型 1.1 泛型的定义与约束 创建泛型方法.委托.接口或类时,需要在名称后增加尖括号及其中的泛型参数,泛 ...

  9. 在linux环境下搭建java web测试环境(非常详细!!)

    一.项目必备软件及基本思路 项目必备:虚拟机:VMware Workstation (已安装linux的 CentOS6.5版本) 项目:java web项目 (必须在本地部署编译后选择项目的webR ...

  10. Java图的邻接矩阵实现

    /** * * 图的邻接矩阵实现 * @author John * * @param <T> */ class AMWGraph<T> { private ArrayList& ...