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 ...
随机推荐
- Android 人脸识别
Android人脸识别技术,可以参考下面的网站. http://www.faceplusplus.com.cn/ 本项目使用的就是该网站的api. 项目具体使用的技术代码 /** * 用来压缩图片的方 ...
- 使用Autofac实现依赖注入注入
依赖注入是什么意思? 依赖倒置 在软件设计原则中,有一种重要的思想叫做依赖倒置.它的核心思想是:不能让高层组件依赖底层组件,而且,不管高层组件和底层组件,两者都应依赖于抽象.那么,这个原则和我们上面的 ...
- Linux 批量删除文件后缀
例子: [zengs@gene CASP9]$ lscasp9.ids T0526 T0538 T0550 T0562 T0574 T0586 T0598 T0610 T0622 T0634T0515 ...
- 了解JavaScript核心精髓(四)
ES6 1.import与require区别 import 是同步导入js模块. require 是异步导入js模块. 2.使用let与const let con1 = 3 //与var作用相似,le ...
- HDU 4747 Mex ( 线段树好题 + 思路 )
参考:http://www.cnblogs.com/oyking/p/3323306.html 相当不错的思路,膜拜之~ 个人理解改日补充. #include <cstdio> #incl ...
- HDU 4669 Mutiples on a circle 动态规划
参考了官方题解给的方法: 对于处理循环,官方给了一种很巧妙的方法: #include <cstdio> #include <cstring> #include <cstd ...
- hdu1712 分组背包 ACboy needs your help
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Charts & canvas & RGBA
Charts & canvas RGBA color let stopFlag = 0; // show Charts const showCharts = (name = "&qu ...
- 第八篇:python基础_8 面向对象与网络编程
本篇内容 接口与归一化设计 多态与多态性 封装 面向对象高级 异常处理 网络编程 一. 接口与归一化设计 1.定义 (1)归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了 ...
- 【bzoj2732】[HNOI2012]射箭 二分+半平面交
题目描述 给出二维平面上n个与y轴平行的线段,求最大的k,使得存在一条形如$y=ax^2+bx(a<0,b>0)$的抛物线与前k条线段均有公共点 输入 输入文件第一行是一个正整数N,表示一 ...