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. 面向对象五大原则(SRP、OCP、LSP、DIP、ISP)

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt173 OO的五大原则是指 1. SRP(Single Responsibil ...

  2. oracle锁表问题解决方法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp52 Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程 ...

  3. Android微信登录、分享、支付

    转载需要著名出处: http://blog.csdn.net/lowprofile_coding/article/details/78004224 之前写过微信登录分享支付第一版: http://bl ...

  4. TP 3.2 笔记 (1)

    1.配置文件分布在好多子模块中 2.I方法 使用指定过滤方法来过滤变量,第三个参数如果是函数名,则会调用该函数进行过滤,(在变量是数组的情况下自动使用array_map进行过滤处理),否则会调用 PH ...

  5. 软工+C(2017第5期) 工具和结构化

    // 上一篇:Alpha/Beta换人 // 下一篇:最近发展区/脚手架 工具/轮子 软件工程/计算机相关专业的一个特点是会使用到众多的工具,工具的使用是从程序猿进化到程序员的一个关键要素.软件工程师 ...

  6. 【Beta】 第二次Daily Scrum Meeting

    一.本次会议为第二次meeting会议 二.时间:13:30AM-13:55AM 地点:禹州 三.会议站立式照片 四.今日任务安排 成员 昨日任务 今日任务 林晓芳 对已完成的功能进行进一步测试,以便 ...

  7. 201521123037 《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 1. String[] list1=str.split(" ...

  8. 201521123015 《Java程序设计》第七周学习总结

    1. 本周学习总结 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: public boolean contains(Object ...

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

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 继承能够动态绑定,运行时能够自动的选择调用方法,十分方便. 2. 书面作业 (1)注释的应用 ...

  10. Java课程设计 学生基本信息管理个人博客

    学生基本信息管理系统个人博客 团队课程设计链接 http://www.cnblogs.com/ll321/p/7067598.html 个人负责模块 负责部分界面设计,处理代码: 处理部分数据库数据. ...