hdu1142 A Walk Through the Forest
来源:互联网 发布:js中的event对象 编辑:程序博客网 时间:2024/05/02 04:55
这题刚开始思考了半天却没想到什么好的办法,最后看了解题报告,发现我从一开始就没有记忆化搜索这方面的想法,我想到了求出每个点到终点的最短路,却没想到用记忆花搜索来解决路径条数
思路:最短路+记忆化搜索
code:
#include <queue>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF = 0x3fffffff;int n,m;int adj[1005],ec;struct edge{ int to,next,w;}p[500001];void add(int from,int to,int w){ p[ec].to=to; p[ec].w=w; p[ec].next=adj[from]; adj[from]=ec++;}int dis[1005];bool inq[1005];void spfa(int s){ int i,to,w; queue<int> q; dis[s]=0; q.push(s); inq[s]=true; while(!q.empty()) { s=q.front(); q.pop(); inq[s]=false; for(i=adj[s];i;i=p[i].next) { w=p[i].w; to=p[i].to; if(dis[to] > dis[s]+w) { dis[to]=dis[s]+w; if(!inq[to]) { inq[to]=true; q.push(to); } } } }}int dp[1005];int dfs(int from){ int i,to,sum=0; if(dp[from]) { return dp[from]; } if(from==2) { return 1; } for(i=adj[from];i;i=p[i].next) { to=p[i].to; if(dis[to] < dis[from]) { sum+=dfs(to); } } dp[from]=sum; return sum;}void init(){ int i; ec=1; for(i=1;i<=n;i++) { dis[i]=INF; } memset(dp,0,sizeof(dp)); memset(adj,0,sizeof(adj)); memset(inq,false,sizeof(inq));}int main(){ int i,a,b,w; while(~scanf("%d",&n) && n) { scanf("%d",&m); init(); for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&w); add(a,b,w); add(b,a,w); } spfa(2); printf("%d\n",dfs(1)); } return 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)
- 天外飞仙之时效爱情
- ruby 正则 $& 和 $'
- 编辑距离
- 14岁初中生—— 踏踏实实的人生也最美
- Linux系统32位与64位GCC编译器基本数据类型长度对照表
- hdu1142 A Walk Through the Forest
- 香港版雙飛人藥水和內地版的區別
- 排序算法1—插入排序、选择排序、冒泡排序
- linux下查看文件编码及修改编码
- poj 2154 Color
- 如何成为顶尖WordPress开发者
- ext4格式 打包/解包
- 想念那一池莲花
- Objective-C KVC机制