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. 深度剖析Redis持久化

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt118 Redis是一种面向"key-value"类型数据 ...

  2. java对象 深度克隆(不实现Cloneable接口)和浅度克隆

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt128 为什么需要克隆: 在实际编程过程中,我们常常要遇到这种情况:有一个对象 ...

  3. 百度AI开放平台- API实战调用

    百度AI开放平台- API实战调用 一.      前言 首先说一下项目需求. 两个用户,分别上传了两段不同的文字,要计算两段文字相似度有多少,匹配数据库中的符合条件的数据,初步估计列出来会有60-1 ...

  4. spring报错NoClassDefFoundError等与第三方jar包导入问题

    今天配置spring,遇到各种报错的问题,做一个小小总结. 1.刚开始我忘了引入commons-logging,报错.--解决方式:下载并引入该jar包 2.spring以及commons-loggi ...

  5. VIN码识别对汽车行业的应用

    汽车VIN码识别: 识别系统到底是什么呢?来~大家往下看,这是易泊时代科技有限公司通过多年的ocr识别技术,与汽车Vin码识别/车架号识别系统相结合,针对机动车配置参数等信息的查询及采集而推出的一款V ...

  6. SNS团队Beta阶段第三次站立会议(2017.05.24)

    1.立会照片 2.每个人的工作 成员 今天已完成的工作 明天计划完成的工作 罗于婕 辅助完善生词本 辅助完成生词本功能 龚晓婷 辅助开发历史记录功能 辅助完善历史记录功能 林仕庄 开发历史记录功能 完 ...

  7. 团队作业4——第一次项目冲刺(Alpha版本) Day5

    首先和助教及老师表示抱歉,博客确实当时就写了,但是一直不算写好,因为这几天卡住了,预计实现的功能实现不了,进度跟不上,现在也在寻求解决方法. 1.站立式会议: 2. Leangoo任务分解图: 3.任 ...

  8. Java 第五周总结

    1. 本周学习总结 2. 书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. 不能. ...

  9. 201521123010 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 常用异常: ①题目5-1 1.1 截图你的提交结果(出现学号) A: 1.2 ...

  10. JAVA2015086第十一周作业

    本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 书面作业 本次PTA作业题集多线程 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了 ...