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) ,现在有六个邮差, ...
随机推荐
- react router路由传参
今天,我们要讨论的是react router中Link传值的三种表现形式.分别为通过通配符传参.query传参和state传参. ps:进入正题前,先说明一下,以下的所有内容都是在react-rout ...
- sls语法:创建file,创建文件夹
http://blog.kukafei520.net/html/2014/942.html /tmp/aaa.txt: file.managed /tmp/salt_test: file.direct ...
- [洛谷P1074] 靶形数独
洛谷题目链接:靶形数独 题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博 ...
- rest service技术选型
MySql workbench下载 http://dev.mysql.com/downloads/workbench/ 最好的8个 Java RESTful 框架 http://colobu.com/ ...
- 【BZOJ1857】【SCOI2010】传送带 [三分]
传送带 Time Limit: 1 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 在一个2维平面上有两条传送带,每一条传送 ...
- POJ 2395 Out of Hay (prim)
题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...
- [bzoj3132]上帝造题的七分钟——二维树状数组
题目大意 你需要实现一种数据结构,支援以下操作. 给一个矩阵的子矩阵的所有元素同时加一个数. 计算子矩阵和. 题解 一看这个题,我就首先想到用线段树套线段树做. 使用二维线段树的错误解法 其实是第一次 ...
- 爆破phpmyadmin小脚本
#!usr/bin/env python #encoding: utf-8 #by i3ekr import requests headers = {'Content-Type':'applicati ...
- adt 运行时,显示no target selected.
检查adt\adt-bundle-windows-x86-20131030\sdk\system-images下面是否有相关image文件.
- 面试===Linux试题及答案
一. 单选题: 1.添加一条静态路由,使到网络196.199.3通过eth2接口出去,用: A. route add -net 196.199.3.0 B. route add -net 196.19 ...