hdu1142 A Walk Through the Forest
来源:互联网 发布:广州淘宝公司招聘 编辑:程序博客网 时间:2024/05/16 04:10
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142
题意:问从office到home有多少路可以走,下一步到家的最小距离必须比现在到家的最短距离小才能到下一步。
思路:先处理出各个点到home的最短距离,再用dfs+记忆化搜索
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#define maxn 1005#define inf 100000using namespace std;int n, m, ans;int Map[maxn][maxn];bool vis[maxn];int dp[maxn];int dist[maxn];void pre(){ memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { Map[i][j] = inf; } }}void Dijkstra(int cur = 2){ vis[cur] = 1; for(int i = 1; i <= n; i++) dist[i] = Map[cur][i]; dist[cur] = 0; for(int i = 2; i <= n; i++) { int k = cur; int minval = inf; for(int j = 1; j <= n; j++) { if(!vis[j] && dist[j] < minval) { minval = dist[j]; k = j; } } vis[k] = 1; for(int j = 1; j <= n; j++) { if(!vis[j]) dist[j] = min(dist[j], dist[k] + Map[k][j]); } }}int DFS(int node){ if(dp[node]) { return dp[node]; } int ans = 0; for(int i = 1; i <= n; i++) { if(Map[node][i] != inf && dist[i] < dist[node]) { ans += DFS(i); } } return dp[node] = ans;}int main(){ while(~scanf("%d", &n) && n) { scanf("%d", &m); pre(); for(int i = 0; i < m; i++) { int s, e, dis; scanf("%d %d %d", &s, &e, &dis); Map[s][e] = Map[e][s] = dis; } Dijkstra(); dp[2] = vis[1] = 1; ans = DFS(1); cout << ans << endl; } return 0;}
阅读全文
0 0
- hdu1142 A Walk Through the Forest
- HDU1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142-A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142-A Walk Through the Forest-最短路+dfs
- hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)
- A Walk Through the Forest(hdu1142,最短路+dfs)
- HDU1142:A Walk Through the Forest(Dijkstra+dfs)
- HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】
- a walk throught the forest(HDU1142)
- A Walk Through the Forest hdu1142 最短路+简单动规
- hdu1142 A Walk Through the Forest 最短路和记忆式搜索dijstra+dfs
- hdu1142 A Walk Through the Forest(前向星+SPFA解法)
- hdu1142 - A Walk Through the Forest(SPFA+记忆化搜索)
- HDU1142——A Walk Through the Forest(spfa+记忆化搜索)
- HDU1142-A Walk Through the Forest(记忆化搜索+SPFA)
- C++STL之序列容器
- UVA 550
- java中集合排序,List<Map>,List<T>类型或者JSONObject的升序和降序排序
- 如果你有一台超级计算机,你会用它来做什么?
- HDU --- 5901 Count primes 【求1e11内的素数模板题】
- hdu1142 A Walk Through the Forest
- Makefile基本命名规则
- json
- css3-transform动画样式
- activemq测试类
- AOP实现的解决流程
- Tensorflow Placeholder 传入值
- tcp四种定时器
- Vuforia sdk 之物体识别