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 (Java/Others)
Total Submission(s): 8850 Accepted Submission(s): 3267
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.
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.
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
//最短路
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
const int N=;
ll g[N][N],vis[N],n,m,k,x,y,z;
ll dp[N],dis[N];
void init()
{
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
g[i][j]=g[j][i]=INF;
}
g[i][i]=;
}
}
void dij(int x)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
dis[i]=g[x][i];
}
vis[x]=;
int v=x;
int minn;
for(int i=;i<=n;i++)
{
minn=INF;
for(int j=;j<=n;j++)
{
if(!vis[j] && minn>dis[j])
{
v=j;
minn=dis[j];
}
}
vis[v]=;
for(int j=;j<=n;j++)
{
if(!vis[j]) dis[j]=min(dis[j],dis[v]+g[v][j]);
}
}
}
ll dfs(ll now)
{
if(dp[now]>) return dp[now];
if(now==) return ;
for(int i=;i<=n;i++)
{
if(g[now][i]!=INF && dis[now]>dis[i])
dp[now]+=dfs(i);
}
return dp[now];
}
int main()
{
while(scanf("%lld",&n) && n)
{
init();
scanf("%lld",&m);
for(int i=;i<m;i++)
{
scanf("%lld%lld%lld",&x,&y,&z);
g[x][y]=g[y][x]=min(g[x][y],z);
}
dij();
memset(dp,,sizeof(dp));
printf("%lld\n",dfs());
}
return ;
}
HDU 1142 A Walk Through the Forest(最短路+dfs搜索)的更多相关文章
- HDU 1142 A Walk Through the Forest(Dijkstra+记忆化搜索)
题意:看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A和B 是相连的,当前在 A 处, 如果 A 到终点的最短距离大于 B 到终点的最短距离,则可以从 A 通往 B 处,问满 ...
- 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 ...
- HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
- hdu 1142 A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...
- 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 ...
- 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 ...
- 题解报告:hdu 1142 A Walk Through the Forest
题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...
- 【解题报告】HDU -1142 A Walk Through the Forest
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
随机推荐
- C语言之基本算法26—佩尔方程求解
//穷举法! /* ====================================================== 题目:求佩尔方程x*x-73*y*y=1的解. =========== ...
- 如何修改Web.Config里面的值
0.先添加 <add key="MAXNUM" value="6" /> 1.读取值 string maxNum = ConfigurationMa ...
- 今日SGU 5.13
SGU 146 题意:就是给你一个长度为l的圈,然后你跑步,每一段给你时间t和速度v,问你最后离起点多远 收获:就是把浮点数转为整数,然后但是会出现精度误差,比如l最多四位小数,那你就加0.00001 ...
- OpenJDK源码研究笔记(十):枚举的高级用法,枚举实现接口,竟是别有洞天
在研究OpenJDK,Java编译器javac源码的过程中,发现以下代码. 顿时发现枚举类竟然也有如此"高端大气上档次"的用法. 沙场点兵(用法源码) com.sun.tools. ...
- CSUOJ 1531 Jewelry Exhibition
Problem G Jewelry Exhibition To guard the art jewelry exhibition at night, the security agency has d ...
- 使用LVS 实现负载均衡的原理。
LVS 负载均衡 负载均衡集群是 Load Balance 集群.是一种将网络上的访问流量分布于各个节点,以降低服务器压力,更好的向客户端提供服务的一种方式.常用 的负载均衡. 开源软件有Nginx. ...
- oracle 01578
http://blog.itpub.net/7728585/viewspace-670597/ http://www.2cto.com/database/201208/149056.html http ...
- 实验记录三 通用输入输出(GPIO)
之前把全部程序都跑了一次后,得到了导师下一步的安排. 例如以下: 1.编写一个程序.实如今LCD上显示一个万年历,包含年月日 星期 还有室内的温度.2.编写一个程序,将原来的交通灯改为跑马灯. 期限是 ...
- USACO milk
/* ID:kevin_s1 PROG:milk LANG:C++ */ #include <iostream> #include <string> #include < ...
- single-page关于vue-router
最近复习了vue,项目实战前有练手,做一个简单的单页面应用. 1.router-view就是我们渲染的组件,router-view被包裹在一个div中,一旦路由跳转,router-view就会被渲染为 ...