hdu 1142(迪杰斯特拉+记忆化搜索)
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7330 Accepted Submission(s): 2687
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
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号点永远要比上一步离2号点近(比如第一个测试样例中2-3 为37 1-2为36,所以肯定不会走3号点)。求符合这样的路径条数。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const int INF = ;
int graph[N][N];
int n,m;
bool vis[N];
int low[N];
int dp[N];
void dijkstra(int n,int pos)
{
memset(vis,,sizeof(vis));
vis[pos]=;
for(int i=; i<=n; i++) low[i]=graph[pos][i];
low[pos] = ;
for(int i=; i<n; i++)
{
int Min=INF;
for(int j=; j<=n; j++)
if(vis[j]==&&Min>low[j])
{
pos=j;
Min=low[j];
}
vis[pos]=;
for(int j=; j<=n; j++)
if(vis[j]==&&low[j]>graph[pos][j]+low[pos])
{
low[j]=graph[pos][j]+low[pos];
}
}
}
int dfs(int s,int n){
if(dp[s]>) return dp[s];
int ans = ;
for(int i=;i<=n;i++){
if(graph[s][i]<INF&&low[s]>low[i]&&i!=s){
ans+=dfs(i,n);
}
}
dp[s] = ans;
return dp[s];
}
int main()
{
while(scanf("%d",&n)!=EOF,n)
{
scanf("%d",&m);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++) graph[i][j]=INF;
}
for(int i=; i<=m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
graph[a][b]=graph[b][a] = c;
}
dijkstra(n,);
memset(dp,,sizeof(dp));
dp[]=;
printf("%d\n",dfs(,n));
}
}
hdu 1142(迪杰斯特拉+记忆化搜索)的更多相关文章
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1428 漫步校园(记忆化搜索,BFS, DFS)
漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...
- hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- hdu 1176免费馅饼(记忆化搜索)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 题意不解释了 简单的记忆化搜索可以拿来练练手,注意要从pos = 5 开始搜索 #include ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU 2476 String painter(记忆化搜索, DP)
题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...
- hdu 5389 Zero Escape(记忆化搜索)
Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi ...
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- hdu 1978 How many ways 记忆化搜索 经典例题
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- CodeForces 771C Bear and Tree Jumps 树形DP
题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...
- 在sqlserver 中如何导出数据库表结构到excel表格中
先建空白excel--在数据库中的左侧找到该表, 选中需要导出的数据--Ctrl+C复制--打开记事本修改编码格式为Unicode-不自动换行保存--Ctrl+A--Ctrl+C,再打开excel-- ...
- WIN10把照片查看器设为默认看图软件
WIN10默认是PHOTO,没有以前WIN7的照片查看器好用,要改回来的方法如下: 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Photo ...
- java_链表反转
定义一个Node节点类 1 public class Node { 2 public int value; 3 public Node next; 4 5 public Node(int value) ...
- Ext JS表单Ext.form.FormPanel
1.表单 对于传统的b/s应用来说,数据录入元素是放在表单<form>标签里面的.而对于ExtJS应用来说,则可以直接使用FormPanel控件来存放表单中的元素.FormPanel继承自 ...
- C# 命名管道
有些场合需要高效率,进行线程间通信,可以使用 C#命名管道.
- reinterpret_cast and const_cast
reinterpret_cast reinterpret意为“重新解释” reinterpret_cast是C++中与C风格类型转换最接近的类型转换运算符.它让程序员能够将一种对象类型转换为另一种,不 ...
- JavaScript中的parseInt和Number函数
函数作用: parseInt将字符串(String)类型转为整数类型. Number() 函数把对象(Object)的值转换为数字. 语法不同: parseInt(string, [radix]) s ...
- SVN基本介绍
SVN是一种项目合作开发的软件,参与项目的人员可以在不同的地方实现文件和目录的超时空共享. 两个重要的概念: 1.配置库(Repository) SVN的核心是配置库,储存所有的数据,配置库按照文件树 ...
- java安全提交笔记【xmind图片】