原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142

题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线回家。由于他要尽快回家,他在选择路线的时候总是要越来越靠近他家。计算符合条件的路线一共有几种。

解题思路:题目要求“路线要越来越靠近家”,也就是说每次选择下一个结点的时候距离家的距离比当前的结点近。首先该结点离家的距离就是该节点到家的最短路径的长度。所以我们先求出所有节点到家的最短路径。(Dijkstra算法)

然后从办公室出发,选择路径的时候要选择相邻且距离更近的点作为下一个点,利用DFS遍历。当前结点的路径条数等于下一个所有可能的结点的路径的总和。而终点的路径为1。

附上代码:

#include<stdio.h>
#define MaxVertexNum 1000 /*顶点数*/
#define MaxWeight 1147483647
typedef int weight;
weight w[MaxVertexNum][MaxVertexNum]={};/*边的关系*/
int d[MaxVertexNum];
int dfs[MaxVertexNum];
/*两点的最短路径*/
void ShortestPath(int vertexnum,int m)
/*从m节点出发,p数组保存路径,d数组保存长度*/
{
int v,min,i,j;
int bo[MaxVertexNum];
for(v=;v<vertexnum;v++)
{
bo[v]=;
d[v]=w[m][v];
}
d[m]=;
bo[m]=;
for(i=;i<vertexnum;i++)
{
min=MaxWeight;
for(j=;j<vertexnum;j++) /*v最近*/
if(!bo[j]&&d[j]<min) {v=j;min=d[j];}
bo[v]=;
for(j=;j<vertexnum;j++)
{
if(!bo[j]&&min+w[v][j]<d[j])
{
d[j]=min+w[v][j];
}
}
}
}
/*根据边的关系输入*/
void input_AdjacencyMatrix(int n)
{
int i,j;
while(n--)
{
scanf("%d%d",&i,&j);
scanf("%d",&w[i-][j-]); /*输入权值*/
w[j-][i-]=w[i-][j-];
}
}
int DFS(int n,int vertexnum)/*表示n节点到1节点的路径数*/
{
int i,s;
if(n==) return ;/*结束条件*/
if(dfs[n]!=-) return dfs[n];/*如果已经计算过则直接调用*/
s=;
for(i=;i<vertexnum;i++)
{
if(w[n][i]!=MaxWeight&&d[n]>d[i])/*相邻且距离更近*/
{
s+=DFS(i,vertexnum);/*路径数的和*/
}
}
dfs[n]=s;/*保存当前遍历结果*/
return s;
}
int main()
{ int i,j,vertexnum,n;
/*freopen("yx.txt","r",stdin);*/
while()
{
scanf("%d",&vertexnum);
if(vertexnum==) return ;
scanf("%d",&n);
for(i=;i<vertexnum;i++)
for(j=;j<vertexnum;j++)
w[i][j]=MaxWeight;/*无穷大*//*初始化*/
input_AdjacencyMatrix(n);
ShortestPath(vertexnum,);
for(i=;i<vertexnum;i++) dfs[i]=-;
printf("%d\n",DFS(,vertexnum));
}
return ;
}

【解题报告】HDU -1142 A Walk Through the Forest的更多相关文章

  1. 题解报告:hdu 1142 A Walk Through the Forest

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...

  2. HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  3. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

  4. HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

    题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...

  5. HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  6. hdu 1142 A Walk Through the Forest (最短路径)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  7. HDU 1142 A Walk Through the Forest(最短路+dfs搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. hdu 1142 A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...

  9. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

随机推荐

  1. Linux下安装Scim-googlepinyin输入法和设置Sublime Text中文输入

    1.安装git sudo apt-get install git http://www.cnblogs.com/perseus/archive/2012/01/06/2314069.html 2.获取 ...

  2. 机器学习之神经网络模型-下(Neural Networks: Representation)

    3. Model Representation I 1 神经网络是在模仿大脑中的神经元或者神经网络时发明的.因此,要解释如何表示模型假设,我们不妨先来看单个神经元在大脑中是什么样的. 我们的大脑中充满 ...

  3. HTML CSS——margin与padding的初学

    下文引自HTML CSS——margin和padding的学习,作者fengyv,不过加入了一些个人的看法. 你在学习margin和padding的时候是不是懵了,——什么他娘的内边距,什么他娘的外边 ...

  4. Struts2 Convention插件的使用(3)方法前的@Action注解

    package com.hyy.action; import org.apache.struts2.convention.annotation.Action; import com.opensymph ...

  5. mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换等

    mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换(上) MP3文件格式(二)---ID3v2 图:ID3V1标签结构 图:ID3V2标签 ...

  6. 修改bigbluebutton白板上传中文乱码

          中文命名的文档,上传是乱码 -- 显示的 打开后, 中文部分是乱码 Comment 1 by project member ffdixon, Nov 08, 2010 Translatio ...

  7. Java-马士兵设计模式学习笔记-观察者模式-OOD线程

    一.概述 1.情景:孩子睡觉,醒后要吃东西,用java模拟此情况 2.设计:child类,Dad类,都继承Runnable,dad线程监视child线程(缺点:因为要监视,所以耗cup资源) 二.代码 ...

  8. Struts2笔记——struts常用标签

    使用struts标签前,首先要配置struts2架构,然后导入标签库,jsp插入如下语句: <%@taglib uri="/struts-tags" prefix=" ...

  9. JSP相对路径与绝对路径探秘

    浏览器端 带杠的 一开始浏览器的地址http://localhost:8080/example/index.jsp 如果写成 <a href="/servlet/TestDBSvl&q ...

  10. JQUERY与JS的区别

    JQUERY与JS的区别 <style type="text/css"> #aa { width:200px; height:200px; } </style&g ...