题目链接

题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B 。

思路 : 先以终点为起点求最短路,然后记忆化搜索。

 //
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
const int INF = << ;
using namespace std ; int N,M ;
int mapp[][] ,pre[],dist[];
bool vis[] ; void spfa(int src)
{
for(int i = ; i <= N ; i++)
{
vis[i] = false ;
dist[i] = INF ;
}
queue<int>Q ;
dist[src] = ;
vis[src] = true ;
Q.push(src) ;
while(!Q.empty())
{
int u = Q.front() ;
Q.pop() ;
vis[u] = false ;
for(int i = ; i <= N ; i++)
{
if(dist[i] > dist[u] + mapp[u][i] && mapp[u][i] != INF)
{
dist[i] = dist[u] + mapp[u][i] ;
if(!vis[i])
{
vis[i] = true ;
Q.push(i) ;
}
}
}
}
}
int dfs(int s)
{
if(pre[s]) return pre[s];
if(s == ) return ;
int cnt = ;
for(int i = ;i <= N ; i++)
{
if(mapp[s][i] < INF && dist[s] > dist[i])
{
if(pre[i]) cnt += pre[i];//已经找过了
else cnt += dfs(i);
}
}
pre[s] = cnt ;
return pre[s];
}
void Init()
{
for(int i = ; i <= N ; i++)
for(int j = ; j <= N ; j++)
{
if(i == j) mapp[i][j] = ;
else mapp[i][j] = INF ;
}
memset(pre,,sizeof(pre)) ;
}
int main()
{
while(cin >> N)
{
if(N == ) break ;
cin >> M ;
Init() ;
int u,v,w ;
while(M--)
{
cin >> u >> v >> w ;
mapp[u][v] = mapp[v][u] = w ;
}
spfa() ;
cout << dfs() << endl ;
}
return ;
}

HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)的更多相关文章

  1. HDU 1142 A Walk Through the Forest(Dijkstra+记忆化搜索)

    题意:看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A和B 是相连的,当前在 A 处, 如果 A 到终点的最短距离大于 B 到终点的最短距离,则可以从 A 通往 B 处,问满 ...

  2. HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

    题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...

  3. HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  5. 题解报告:hdu 1142 A Walk Through the Forest

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...

  6. HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...

  7. hdu1428 spfa+记忆化搜索

    题意:      题意坑爹,很容易误认成是做短路的条数,题意是给你一个图,让你从起点走到终点,问你有多少种走法,但有一个限制,假如你想从a走到b,必须满足终点到b的最短距离小于终点到a的最短距离. 思 ...

  8. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

  9. hdu 1142 A Walk Through the Forest (最短路径)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

随机推荐

  1. android----Java DES加密算法工具类

    DESUtil类 public class DESUtil { private static byte[] iv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (by ...

  2. poj 2312 Battle City

    题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...

  3. 在WIN7下安装运行mongodb 1)、下载MongoDB

    1).下载MongoDB http://downloads.mongodb.org/win32/mongodb-win32-i386-2.4.5.zip 下载Windows 32-bit版本并解压缩, ...

  4. [转]linux时间同步

    转自:http://www.jotop.com/2012/vpsinfo_0525/439.html 美国的vps大多都是国外的时间,让我们的程序总是不适应.那么如何调整linux的时间为北京时间?修 ...

  5. ios开发--KVO浅析

    目标:监听NSMutableArray对象中增加了什么 代码如下: - (void)viewDidLoad { [super viewDidLoad]; self.dataArray = [NSMut ...

  6. dev 激活没有权限问题

    用管理员身份打开 Microsoft Visual Studio 2010-->Visual Studio Tools-->Visual Studio 命令提示(2010) 然后输入一下命 ...

  7. [转]unzip解压windows zip乱码的处理

    [转]unzip解压windows zip乱码的处理 http://blog.sina.com.cn/s/blog_6c9d65a101012gz0.html 朋友从windows传过来的zip文件, ...

  8. Qt 按键长按的处理

    keyPressEvent()部分代码: if (e->key() == Qt::Key_A && e->isAutoRepeat()) {   if (!mPressFl ...

  9. N!大整数阶乘问题

    问题:求N!阶乘,1<=N<10000 思路:windows下面visual 6.0中c一个整型占4个字节(自己可以try一下,printf("%d", sizeof( ...

  10. 监听文本框输入开发仿新浪微博限制输入字数的textarea插件

    监听文本框输入 Firefox.Chrome.IE9,IE10 均支持 oninput 事件,此外所有版本的 IE 均支持 onpropertychange 事件. oninput 事件在用户输入.退 ...