hdu_A Walk Through the Forest ——迪杰特斯拉+dfs
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
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.
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.
the number of different routes through the forest. You may assume that this
number does not exceed 2147483647
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
4
#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的更多相关文章
- 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 ...
- 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 ...
- HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)
题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...
- 1018 Public Bike Management (30分) (迪杰斯特拉+dfs)
思路就是dijkstra找出最短路,dfs比较每一个最短路. dijkstra可以找出每个点的前一个点, 所以dfs搜索比较的时候怎么处理携带和带走的数量就是关键,考虑到这个携带和带走和路径顺序有关, ...
- 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 ...
- hdu 1142(迪杰斯特拉+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- POJ 2502 Subway(迪杰斯特拉)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6692 Accepted: 2177 Descriptio ...
- C#迪杰斯特拉算法
C#迪杰斯特拉算法 网上有许多版本的,自己还是写一个理解点 Dijkstra.cs public class Dijkstra { private List<Node> _nodes; p ...
- C++迪杰斯特拉算法求最短路径
一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...
随机推荐
- 学习react,动手实现一个小demo(仿知乎问答)
学习react也有一周的时间,最近自己做了个仿知乎问答的小demo,项目源码在github上:https://github.com/yang302/reactQa 使用技术:bower+gulp+re ...
- Angular.js学习笔记 (一)
- angular中最重要的概念是指令(directive)- ng-model 是双向数据绑定的指令,效果就是将当前元素的value属性和模型中的[user.name]建立绑定关系### 模块(Mo ...
- cookie,session,token的定义及区别
参考了很多文章总结的. 1.cookie(储存在用户本地终端上的数据) 服务器生成,发送给浏览器,浏览器保存,下次请求同一网站再发送给服务器. 2.session(会话) a.代表服务器与浏览器的一次 ...
- Docker for Mac与IntelliJ Docker Integration插件的兼容性问题
笔者在自己的Mac上安装的是Docker for Mac,而不是Docker Toolbox. 这两者最主要的区别在于Docker for Mac用HyperKit作为虚拟化解决方案而不是Virtua ...
- JS存在性
var myObject = { a:2 }; ("a" in myObject);//true ("b" in myObject);//false myObj ...
- 你想要的都在这里,ASP.NET Core MVC四种枚举绑定方式
前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
- Spring事务隔离级别与传播机制详解,spring+mybatis+atomikos实现分布式事务管理
原创说明:本文为本人原创作品,绝非他处转载,转账请注明出处 1.事务的定义:事务是指多个操作单元组成的合集,多个单元操作是整体不可分割的,要么都操作不成功,要么都成功.其必须遵循四个原则(ACID). ...
- Machine Learning——Supervised Learning(机器学习之监督学习)
监督学习是指:利用一组已知类别的样本调整分类器的参数,使其达到所要求性能的过程. 我们来看一个例子:预测房价(注:本文例子取自业界大牛吴恩达老师的机器学习课程) 如下图所示:横轴表示房子的面积,单位是 ...
- 简单XSS跨站脚本攻击实验
原理:恶意Web用户将代码植入到提供给其它用户使用的页面中,如果程序没有经过过滤或者过滤敏感字符不严密就直接输出或者写入数据库.合法用户在访问这些页面的时候,程序将数据库里面的信息输出,这些恶意代码就 ...
- 蓝桥杯-有理数类-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...