A Walk Through the Forest

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 1
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
 
题意:Jimmy工作压力大,为了放松,他喜欢走路回家。而为了不太晚到家,他又得朝着家的方向走,也就是说必须离家越来越近。给定一个无向图,求有多少条路径回家。
 
分析:先用迪杰特斯拉算法求出各结点离源点(家)的最短距离,然后dfs求出路径总数。
 
时间超限代码(dfs需优化)
 #include <cstdio>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int maxn=;
vector<int> g[maxn];
struct node
{
int num,dis;
};
int n,m;
int cost[maxn][maxn],d[maxn];
int ans; bool operator<(const node& n1,const node& n2)
{
return n1.dis<n2.dis;
} void dij()
{
fill(d+,d+n+,INF);
d[]=;
priority_queue<node> que;
que.push(node{,});
while(!que.empty())
{
int x=que.top().num;
que.pop();
for(int i=;i<g[x].size();i++)
{
int u=x;
int v=g[x][i];
if(d[v]>d[u]+cost[u][v])
{
d[v]=d[u]+cost[u][v];
que.push(node{v,d[v]});
}
}
}
} void dfs(int x)
{
if(x==)
{
ans++;
return ;
}
for(int i=;i<g[x].size();i++)
{
int fx=g[x][i];
if(d[fx]<d[x])
dfs(fx);
}
} int main()
{
while(scanf("%d",&n),n)
{
scanf("%d",&m);
ans=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
cost[i][j]=(i==j)?:INF;
}
for(int i=;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b);
g[b].push_back(a);
cost[a][b]=cost[b][a]=c;
}
dij();
d[]=;
dfs();
printf("%d\n",ans);
for(int i=;i<=n;i++)
g[i].clear();
ans=;
}
return ;
}

优化分析:存在对某些结点进行了多次dfs,导致时间耗费过多。考虑采用记忆化搜索。我们用sum[i]表示从i出发有多少条到家的路径,对于已知sum[i]的结点i就不需要再次dfs,这样dfs的次数就只有n-1次(n为结点数)。

优化后代码:用时514ms

 #include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int maxn=;
vector<int> g[maxn];//图的邻接表表示
struct node
{
int num,dis;
};
int n,m;
int cost[maxn][maxn],d[maxn];
int sum[maxn]; bool operator<(const node& n1,const node& n2)//重载运算符
{
return n1.dis<n2.dis;
} void dij()
{
fill(d+,d+n+,INF);//下标从1开始
d[]=;
priority_queue<node> que;
que.push(node{,});
while(!que.empty())
{
int x=que.top().num;
que.pop();
for(int i=;i<g[x].size();i++)
{
int u=x;
int v=g[x][i];
if(d[v]>d[u]+cost[u][v])
{
d[v]=d[u]+cost[u][v];
que.push(node{v,d[v]});
}
}
}
} void dfs(int x)
{
for(int i=;i<g[x].size();i++)
{
int fx=g[x][i];
if(d[fx]<d[x])
{
if(sum[fx]>)
{
sum[x]+=sum[fx];
}
else
{ dfs(fx);
sum[x]+=sum[fx];
}
}
}
// return ;
} int main()
{
while(scanf("%d",&n),n)
{
scanf("%d",&m);
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
cost[i][j]=(i==j)?:INF;
}
for(int i=;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b);
g[b].push_back(a);
cost[a][b]=cost[b][a]=c;
}
dij();
sum[]=;
dfs();
printf("%d\n",sum[]);
for(int i=;i<=n;i++)
g[i].clear();
}
return ;
}

此题bug过的地方

(1)fill(d+1,d+1+n,INF),一开始没注意下标是从1开始,写成了fill(d,d+n,INF)。dij函数bug。

(2)每组用例输出后没有清空图,导致下一组用例的图多了许多不属于自己的边和顶点。

hdu_A Walk Through the Forest ——迪杰特斯拉+dfs的更多相关文章

  1. PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]

    1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...

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

  3. HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

    题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...

  4. 1018 Public Bike Management (30分) (迪杰斯特拉+dfs)

    思路就是dijkstra找出最短路,dfs比较每一个最短路. dijkstra可以找出每个点的前一个点, 所以dfs搜索比较的时候怎么处理携带和带走的数量就是关键,考虑到这个携带和带走和路径顺序有关, ...

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

  6. hdu 1142(迪杰斯特拉+记忆化搜索)

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

  7. POJ 2502 Subway(迪杰斯特拉)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6692   Accepted: 2177 Descriptio ...

  8. C#迪杰斯特拉算法

    C#迪杰斯特拉算法 网上有许多版本的,自己还是写一个理解点 Dijkstra.cs public class Dijkstra { private List<Node> _nodes; p ...

  9. C++迪杰斯特拉算法求最短路径

    一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...

随机推荐

  1. Twitter数据抓取的方法(二)

    Scraping Tweets Directly from Twitters Search Page – Part 2 Published January 11, 2015 In the previo ...

  2. ios 网络/本地播放器

    推荐播放器: LRLAVPlayer相对易懂好修改,调整添加内容. https://github.com/codeWorm2015/videoPlayer NSString*path=[[NSBund ...

  3. .exe简单的更新软件demo

    百度网盘源码加文件:http://pan.baidu.com/s/1qYe2Vgg 功能:通过网站更新用户的软件,需要联网,也可以通过本地网站更新局域网用户软件. 根本实现:1.一个网站(本地就可以) ...

  4. 3 安装Zookeeper

    cnblogs-DOC 1.服务器环境 2.安装Redis3.安装Zookeeper4.安装MPush5.安装Alloc服务6.完整测试7.常见问题 从官网直接下载Zookeeper最新版本(Zook ...

  5. [Oracle]Audit(二)--清理Audit数据

    在上一篇,初步了解了Audit的作用以及如何使用Audit,本篇记录如何手动清理Audit数据. (一) 概述 Audit的数据主要存储在sys.aud$表中,该表默认位于system表空间中,我们根 ...

  6. 关于 __proto__和prototype的一些理解

    var Person = function(name) {}; Person.prototype.say = function() { console.log("Person say&quo ...

  7. meta 整理

    < meta > 元素 概要 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 we ...

  8. 关于Java内存管理的几个小技巧

    这里将介绍几则Java内存管理的小技巧,让你让你从Java入门开始告别陋习,为Java程序提速.有不少人都说"Java完了,只等着衰亡吧!",为什么呢?最简单的的例子就是Java做 ...

  9. css过渡模块和2d转换模块

    今天,我们一起来研究一下css3中的过渡模块.2d转换模块和3d转换模块 一.过渡模块transition (一)过度模块的三要素: 1.必须要有属性发生变化 2.必须告诉系统哪个属性需要执行过渡效果 ...

  10. DELL Precision Tower7910重装系统+开机出现GRUB界面如何处理

    想给实验室的工作站重新装个Win7系统,因为以前并没装过工作站的系统,发现和普通的电脑装系统还是有些不一样的.主要的问题就在于主板的不同. 尝试了老毛桃U盘启动盘安装,结果在WinPE里面提示找不到硬 ...