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 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...
随机推荐
- Twitter数据抓取的方法(一)
Scraping Tweets Directly from Twitters Search Page – Part 1 Published January 8, 2015 EDIT – Since I ...
- yii2.0套用模板问题
载入视图 在控制器中: $this->render(); 会加载布局 $this->renderPartial(); 不会加载布局(也不能载入框架自带的jquery等) Yii2 选择布局 ...
- 有关DeadLock的文章列表
SET STATISTICS PROFILE ON 显示query的执行过程 Troubleshooting Deadlocks Understanding Locking in SQL Se ...
- STM32F4XX与STM32F0XX编程差别
//普通管脚初始化 /*****************************************************************************STM32F0***** ...
- css常用的属性方法 上篇
自己是从java后台自学转前端的,所以平时一些简单的css+html就不写了,列出的都是新手常用的一些属性,会持续更新,大神勿喷,留给新手做个参考! 尤其是跟我一样自学前端的. 背景关联 ba ...
- PHP获取Post的原始数据方法小结(POST无变量名)
From : http://blog.csdn.net/hotdigger/article/details/6456240 一般我们都用$_POST或$_REQUEST两个预定义变量来接收POST ...
- Ubuntu14.04双网卡主备配置
近日有个需求,交换机有两台,做了堆叠,服务器双网卡,每个分别连到一台交换机上.这样就需要将服务器的网卡做成主备模式,以增加安全性,使得当其中一个交换机不通的时候网卡能够自动切换. 整体配置不难,网上也 ...
- 图论算法-Dijkstra
原理 Dijkstra是一个神奇的最短路径算法,它的优点在于它可以稳定的时间内求出一张图从某点到另一点的距离.它的工作原理非常简单,思路类似于广搜.在搜索前,将每个点的颜色设为白色,第一次将源点Ins ...
- ThinkPHP3.2——基础
1.1.获取ThinkPHP 3.2 获取ThinkPHP的方式很多,官方网站(http://thinkphp.cn)是最好的下载和文档获取来源. 官网提供了稳定版本的下载:http://thinkp ...
- 关于cisco ccp 或sdm管理gns3中思科路由器的成功分享
本来工作环境中有一台c1841,闲来无事,升级了最新的IOS=c1841-adventerprisek9-mz.151-4.M6.bin,在xp虚拟机中安装sdm(新windows系统不支持)和在wi ...