HDU 1142 A Walk Through the Forest (SPFA+记忆化搜索)
2014-07-14 21:22
489 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142
题意及思路:找一共有多少条符合题意的路。能够从点A走到点B的要求是:点A到终点的最短路 > 点B到终点的最短路。 也就是说:从终点出发,求每一个点的最短路,然后那些最短路的值记录起来,作为能否通过的判断条件。最后用记忆化搜索来搜索出一共多少条符合要求的路
不太懂如何进行记忆化搜索,找了份题解,-_-|||
转自:http://gzhu-101majia.iteye.com/blog/1151203
题意及思路:找一共有多少条符合题意的路。能够从点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 ; int map , dist , s ; bool visit ; 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; }
相关文章推荐
- hdu1142 - 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+记忆化搜索)
- hdu1142 A Walk Through the Forest
- 【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest(Dijkstra+Dfs(第一次用记忆化搜索))
- Hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest(spfa+深搜)
- hdu 1142 A Walk Through the Forest
- 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(图论Dijkstra)
- A Walk Through the Forest HDU 1142
- 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