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 ...
 
随机推荐
- SpringMVC---applicationContext.xml配置详解
			
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
 - Java语言基础---两变量间的交换
			
使用中间变量交换两个变量的值 int a = 10 , b = 11 , m; m = a; a = b; b = m; 不使用中间变量交换两个变量的值 int a = 10; int b = 11; ...
 - python几个复习例子
			
1.实现1-100的所有的和,程序代码如下: sum = 0 for i in xrange(1,101): sum +=i print (sum) 程序运行结果: 2.实现1-500所有奇数的和,程 ...
 - nodejs安装&bower 安装
			
1.进入官网下载:https://nodejs.org/en/ 2.直接进行安装,可以将安装路径设置为:D:\nodejs 3.进入node.js command prompt 命令窗口 4.检测是否 ...
 - PAT、PMT、SDT详解
			
下面针对解复用程序详细分析一下PAT,PMT和SDT三类表格的格式. 如下图,四个频道复用 PAT---Program Association Table,节目关联表 .PAT表携带以下信息: (1) ...
 - 使用Oracle绿色客户端(InstantClient)连接远程Oracle的配置方法
			
非常简单的配置,网上一搜,有很多,但是还是想记录下来,说不定以后需要了,直接进自己的博客看看也好啊. 下载了PLSQL Developer 11,安装好了发现不能连接远程数据库,但是又不想安装orac ...
 - 【情人节礼物】纯js脚本打造精美3D玫瑰
			
情人节就要来临了,这是用代码做出的玫瑰花,这才是程序员送给女友的最好情人节礼物...(提示:在不同浏览器下观看效果.速度会有很大的不同) 代码如下: <!doctype html> < ...
 - CSS UNIT 详解以及最佳实践
			
分类 ■ 绝对长度(Absolute units):cm,mm,in,pt,pc 绝对单位之间的换算:1in = 2.54cm=25.4mm=72pt=6pc 绝对长度在css中的表现和其他地方 ...
 - Python 协程与事件循环
			
Table of Contents 前言 协程 async & await 事件循环 asyncio 的事件循环 结语 参考链接 前言 Python 标准库 asyncio 是我目前接触过的最 ...
 - hdu2010(dfs+剪枝)
			
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...