HDU 1142 A Walk Through the Forest (SPFA+记忆化搜索)
来源:互联网 发布:软件企业平均工资 编辑:程序博客网 时间:2024/04/28 12:41
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142
题意及思路:找一共有多少条符合题意的路。能够从点A走到点B的要求是:点A到终点的最短路 > 点B到终点的最短路。 也就是说:从终点出发,求每一个点的最短路,然后那些最短路的值记录起来,作为能否通过的判断条件。最后用记忆化搜索来搜索出一共多少条符合要求的路
不太懂如何进行记忆化搜索,找了份题解,-_-|||
转自:http://gzhu-101majia.iteye.com/blog/1151203
#include <iostream>#include <stdio.h>#include <memory.h>#include <queue>#include <vector>using namespace std;const int INF = 1e9;const int N = 1005;vector<int> road[N];int map[N][N], dist[N], s[N];bool visit[N];int n, t, sum;void init(){ int i, j; sum = 0; memset(s, 0, sizeof(s)); for(i = 0; i <= n; i++) road[i].clear(); for(i = 0; i <= n; i++) for(j = 0; j <= n; j++) if(i == j) map[i][j] = 0; else map[i][j] = INF;}void input(){ int a, b, d; while(t--) { scanf("%d %d %d", &a, &b, &d); if(d < map[a][b]) { map[a][b] = map[b][a] = d; //双向的 road[a].push_back(b); road[b].push_back(a); } }}void spfa(){ int i, now; memset(visit, false, sizeof(visit)); for(i = 1; i <= n; i++) dist[i] = INF; dist[2] = 0; queue<int> Q; Q.push(2); visit[2] = true; while(!Q.empty()) { now = Q.front(); Q.pop(); visit[now] = false; for(i = 1; i <= n; i++) { if(dist[i] > dist[now] + map[now][i]) { dist[i] = dist[now] + map[now][i]; if(visit[i] == false) { Q.push(i); visit[i] = true; } } } }}int dfs(int now) //记忆化搜索{ int i; if(now == 2) return 1; //找到终点,返回1(1条路) if(s[now]) return s[now]; //该点已经走过,返回:过该点有多少条路 for(i = 0; i < road[now].size(); i++) { if(dist[now] > dist[ road[now][i] ]) { s[now] += dfs(road[now][i]); } } return s[now]; //返回:该点一共可以有多少条路}int main(){ while(scanf("%d", &n), n) { scanf("%d", &t); init(); input(); spfa(); sum = dfs(1); //sum一共多少条路,sum = s[1] 也行 printf("%d\n", sum); } return 0;}
0 0
- hdu 1142 A Walk Through the Forest(spfa求最短路+记忆化搜索)
- HDU 1142 A Walk Through the Forest (SPFA+记忆化搜索)
- HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)
- hdu 1142/hrbust 1347 A Walk Through the Forest/森林中的路【SPFA+记忆化搜索】
- HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】
- hdu 1142 A Walk Through the Forest Dijkstra+记忆搜索
- HDU 1142 A Walk Through the Forest (SPFA+记忆化DFS)
- hdu 1142 A Walk Through the Forest (最短路 + 记忆化搜索)
- 【HDU】1142 A Walk Through the Forest 最短路+记忆化搜索
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- hdu 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索)
- HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)
- hdu-1142-A Walk Through the Forest-最短路+记忆化搜索
- hdu 1142 A Walk Through the Forest(最短路+记忆化搜索)
- hdu 1142 A Walk Through the Forest (digkstra+记忆化搜索)
- HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)
- HDU 1142 A Walk Through the Forest (dijkstra+记忆化搜索)
- HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
- c语言入门(三)分支结构
- VS2005 常用断点调试
- POJ1631 Bridging signals 最长上升子序列
- 2014年7月14晚笔记
- 算法题002--Max Points on a Line
- HDU 1142 A Walk Through the Forest (SPFA+记忆化搜索)
- c语言入门(四)循环
- vba 学习(1)
- 使用UE4发布安卓平台游戏
- 1013: [JSOI2008]球形空间产生器sphere (高斯消元)
- xlrd与xlwt的使用
- 鸽巢原理的模拟
- Windows下挂载虚拟机中的目录为网络磁盘
- Struts2核心工作原理解析