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. 如何选择适合自己的CMS建站系统

             在互联网发展突飞猛进的今天,可谓”得市场者得天下”,而电商已成为占据市场速度最快.范围最广的手段,进而网站建设也逐渐引起商家重视,并深入到了企业和个体的发展战略之中. 如今企业建站已 ...

  2. jQuery扩展函数设置所有对象只读

    jQuery(function ($) {             $.fn.disable = function () {                 return this.each(func ...

  3. GPIO的配置过程

    今天看到一篇很好的博文,,看这里:http://www.cnblogs.com/crazyxu/archive/2011/10/14/2212337.html 下面总结一下,加深一下理解. 要使用GP ...

  4. Python的内置函数open()的注意事项

    用法 : open("file_address","open_mode")例子 : f = open("D:\PycharmProjects\log. ...

  5. 《分布式Java应用之基础与实践》读书笔记二

    远程调用方式就是尽可能地使系统间的通信和系统内一样,让使用者感觉调用远程同调用本地一样,但其实没没有办法做到完全透明,例如由于远程调用带来的网络问题.超时问题.序列化/反序列化问题.调式复杂的问题等. ...

  6. 读书笔记 effective c++ Item 55 让你自己熟悉Boost

    你正在寻找一个高质量的,开源的,与平台和编译器无关的程序库的集合?看一下Boost吧.想加入一个由雄心勃勃的,充满天赋的正致力于最高水平的程序库设计和实现工作的C++程序员们组成的团体么?看一下Boo ...

  7. 随应潮流-基于ABP+Angulsrjs现代化应用软件开发框架(2)-abp说明

    前言 上周未发布完<基于ABP+Angulsrjs现代化应用软件开发框架(1)-总体介绍> 文章后,好多朋友问了我一些ABP的问题,并且希望我开源我的项目源码,向朋友们说一下,我项目的源码 ...

  8. Python中使用Mysql(安装篇)

    准备工作 import MySQLdb Linux系统自带了Python,但并不是都有这个包,至少我每次拿到一台全新的服务器时候,都发现没有装这个包. 这个东西的下载地址是 http://source ...

  9. PHP die() 函数

    die() 函数输出一条消息,并退出当前脚本. 该函数是 exit() 函数的别名.

  10. MongoDB副本集的常用操作及原理

    本文是对MongoDB副本集常用操作的一个汇总,同时也穿插着介绍了操作背后的原理及注意点. 结合之前的文章:MongoDB副本集的搭建,大家可以在较短的时间内熟悉MongoDB的搭建和管理. 下面的操 ...