hdu1142 A Walk Through the Forest —— dijkstra + 记忆化搜索
来源:互联网 发布:学法语的软件 编辑:程序博客网 时间:2024/05/16 06:57
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <vector>#include <map>#include <set>#include <queue>#include <sstream>#include <algorithm>using namespace std;#define pb push_back#define mp make_pair#define ms(a, b) memset((a), (b), sizeof(a))#define eps 0.0000001typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int maxn = 2000000+10;const int mod = 1e9+7;int n,m,cost[1005][1005],dis[1005],vis[1005],dp[1005];void dijkstra(int st){ int i,j,k; memset(vis,0,sizeof(vis)); for(i = 1; i<=n; i++) { dis[i] = cost[st][i]; } dis[st] = 0; vis[st] = 1; for(i = 2; i<=n; i++) { int minn = INF; for(j = 1; j<=n; j++) { if(!vis[j]&&dis[j]<minn) { minn = dis[j]; k = j; } } vis[k] = 1; for(j = 1; j<=n; j++) { if(!vis[j]) dis[j] = min(dis[j], dis[k] + cost[k][j]); } }}int DFS(int st){ int sum = 0; if(dp[st]) return dp[st]; if(st==2) return 1; for(int i = 1; i<=n; i++) { if(cost[st][i]!=INF && dis[st]>dis[i]) sum += DFS(i); } dp[st] = sum; return dp[st];}int main(){ int i,j,u,v,w; while(scanf("%d",&n) && n) { for(i = 1; i<=n; i++) { for(j = 1; j<=n; j++) { cost[i][j] = (i==j?0:INF); } } scanf("%d",&m); for(i = 1; i<=m; i++) { scanf("%d%d%d",&u,&v,&w); cost[u][v]=cost[v][u] = w; } dijkstra(2); memset(dp,0,sizeof(dp)); printf("%d\n",DFS(1)); } return 0;}
0 0
- hdu1142 A Walk Through the Forest —— dijkstra + 记忆化搜索
- HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】
- HDU1142——A Walk Through the Forest(spfa+记忆化搜索)
- hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)
- 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+记忆化搜索)
- hdu 1142 A Walk Through the Forest Dijkstra+记忆搜索
- HDU1142:A Walk Through the Forest(Dijkstra+dfs)
- hdu1142 A Walk Through the Forest 最短路和记忆式搜索dijstra+dfs
- ZOJ Walk Through the Forest (dijkstra + BFS || 记忆化搜索)
- UVA 10917 Walk Through the Forest (dijkstra+记忆化搜索)
- hdu 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索)
- HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)
- HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)
- HDU 1142 A Walk Through the Forest (dijkstra+记忆化搜索)
- HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
- Python读取中文路径
- 从键盘输入一个大于9999的整数,然后把每个数位上的数累加
- 取数字问题
- Hi,大家好,酝酿了好久终于决定开通自己的技术博客,当然也会也大家分享一些所见所闻,还请读者不吝赐教,共同进步!
- JZOJ 3632. 【汕头市选2014】舞伴
- hdu1142 A Walk Through the Forest —— dijkstra + 记忆化搜索
- 快速幂精解
- 通配符匹配
- vxlan的使用
- Angular2是Angular1的版本升级吗?
- hadoop 的live俄几点显示是0问题
- HDU1172 猜数字
- 活动选择问题(贪心)
- MapReduce的容错机制