HDU1142 A Walk Through the Forest(dijkstra)
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7733 Accepted Submission(s): 2851
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
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
题目意思是说一个人要从上班的地方回到家里,途中会经过一些地方,按下面规则问他回家会有几种路线:
题目已经定义好1为上班地方2为家,每个地点之间的距离都已经知道,哪么如果从A到B的一条路,可以走的条件
是B到2(家)的路程必须小于从A到2(家)的路程,其实就是A到家的最短路径必须大于B到家的最短路径。
默认一定可以走到家,也就是一定会有一种路线。最后是让你计算有几种路线。可以用迪杰斯特拉算法找一下所有点到终点的最短路,然后DFS搜一下就行了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
int n;//十字路口数
int w[][];
int dist[],dp[];
int s[];
void dijkstra(int v)//迪杰斯特拉算法
{
int i,j,mins,index;
for(i=; i<=n; i++)
{
dist[i] = w[i][v];
s[i] = ;
}
dist[v] = ;
s[v] = ;
for(i=; i<n; i++)
{
mins = ;
for(j=; j<=n; j++)
{
if(s[j]== && dist[j]<mins)
{
mins = dist[j];
index = j;
}
}
if(mins == )//注意 若没有 会在中间溢出
break;
s[index] = ;
for(j=; j<=n; j++)
{
if(s[j]== && dist[j]>dist[index]+w[j][index])
dist[j] = dist[index]+w[j][index];
}
}
} int dfs(int v)//记忆法深搜
{
if(dp[v] != -)
return dp[v];
if(v == )
return ;
int i,temp,sum=;
for(i=; i<=n; i++)
{
if(w[v][i]!= && dist[v] > dist[i])//有路相通而且要去的i点到终点站的距离要比v到终点站的距离小
{
temp = dfs(i);
sum += temp;
}
}
dp[v] = sum;
return sum;
} int main()
{
while(cin>>n && n)
{
int i,j,d,m;
cin>>m;
for(i=; i<=n; i++)
{
dp[i] = -;
for(j=; j<=n; j++)
w[i][j] = ;
}
while(m--)
{
scanf("%d%d%d",&i,&j,&d);
w[i][j] = w[j][i] = d;//无向图
}
//求出各点到终点站的最短距离
dijkstra();//2为终点站
dfs();//从1出发
cout<<dp[]<<endl;
}
return ;
}
HDU1142 A Walk Through the Forest(dijkstra)的更多相关文章
- HDU1142 A Walk Through the Forest(最短路+DAG)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- UVA-10917 Walk Through the Forest (dijkstra+DP)
题目大意:n个点,m条边的无向图.一个人从起点到终点按照下面的走法:从A走向B当A到终点的最小距离比B到终点的最小距离大时.问从起点到终点有多少路径方案. 题目分析:先用dijkstra预处理出终点到 ...
- UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)
用新模板阿姨了一天,换成原来的一遍就ac了= = 题意很重要..最关键的一句话是说:若走A->B这条边,必然是d[B]<d[A],d[]数组保存的是各点到终点的最短路. 所以先做dij,由 ...
- 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)
迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...
- 最短路径之迪杰斯特拉(Dijkstra)算法
迪杰斯特拉(Dijkstra)算法主要是针对没有负值的有向图,求解其中的单一起点到其他顶点的最短路径算法.本文主要总结迪杰斯特拉(Dijkstra)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...
- 理解最短路径——迪杰斯特拉(dijkstra)算法
原址地址:http://ibupu.link/?id=29 1. 迪杰斯特拉算法简介 迪杰斯特拉(dijkstra)算法是典型的用来解决最短路径的算法,也是很多教程中的范例,由荷兰计算机科 ...
- 图论——迪杰斯特拉算法(Dijkstra)实现,leetcode
迪杰斯特拉算法(Dijkstra):求一点到另外一点的最短距离 两种实现方法: 邻接矩阵,时间复杂度O(n^2) 邻接表+优先队列,时间复杂度O(mlogn)(适用于稀疏图) (n:图的节点数,m:图 ...
- 算法-迪杰斯特拉算法(dijkstra)-最短路径
迪杰斯特拉算法(dijkstra)-最短路径 简介: 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中 ...
- 数据结构与算法——迪杰斯特拉(Dijkstra)算法
tip:这个算法真的很难讲解,有些地方只能意会了,多思考多看几遍还是可以弄懂的. 应用场景-最短路径问题 战争时期,胜利乡有 7 个村庄 (A, B, C, D, E, F, G) ,现在有六个邮差, ...
随机推荐
- 【题解】NOI2014购票
实际上是一个不完美算法……cogs上面A不掉(爆栈啦).感谢机房大佬PPY的指点,现在也写出来供大家参考参考,理解起来应该是比较简单的一种. 我们首先get出斜率优化方程: \(dp[v] = dis ...
- HDU 5671 矩阵
Matrix Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 使用命令wsimport生成WebService客户端
使用命令wsimport生成WebService客户端 wsimpost命令有几个重要的参数: -keep:是否生成java源文件 -d:指定输出目录 -s:指定源代码输出目录 -p ...
- JS中二维数组的声明
var myarr=new Array(); //先声明一维 for(var i=0;i<2;i++){ //一维长度为2 myarr[i]=new Array(); //再声明二维 for(v ...
- WITH AS 使用
WITH AS 含义: WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让S ...
- java JDK动态代理的机制
一:前言 自己在稳固spring的一些特性的时候在网上看到了遮掩的一句话“利用接口的方式,spring aop将默认通过JDK的动态代理来实现代理类,不适用接口时spring aop将使用通过cgli ...
- [洛谷P2113] 看球泡妹子
洛谷题目链接:看球泡妹子 题目背景 2014年巴西世界杯开幕了,现在满城皆是世界杯,商家们利用它大赚一笔,小明和小红也借此机会增进感情. 题目描述 本届世界杯共有N支球队,M场比赛.男球迷小明喜欢看比 ...
- [洛谷P2124] 奶牛美容
洛谷题目链接:奶牛美容 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 6 16 ................ ..XXXX....XXX... ...XXXX... ...
- GDSOI2015的某道题目
分析: 看到这个$3^i$就觉得很奇怪的样子...为什么一定要是$3^i$...而且不能重复使用... 不能重复使用就代表不会产生进位,那么一定是若干个$3^i$相加减的式子... 仔细观察,我们发现 ...
- 端到端测试,protractor测试的教程
之前我们介绍了如何测试某段js代码的逻辑是否正确,考虑的情况是否全面,但是在ui界面上我们每次做好的功能都要自己去填写内容,点击按钮等,那么是否存在自动化测试的工具呢,让这些事情可以自动完成,答案是肯 ...