A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4386    Accepted Submission(s): 1576

Problem Description
Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable. The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
 
Input
Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.
 
Output
For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647
 
Sample Input

5 6

1 3 2

1 4 2

3 4 3

1 5 12

4 2 34

5 2 24

7 8

1 3 1

1 4 1

3 7 1

7 4 1

7 5 1

6 7 1

5 2 1

6 2 1

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<queue>
using namespace std;
const int Max=;
const int HH=(<<)-;
int f[][];
__int64 val[];
int dis[];
bool in_queue[];
int n,m;
void bfs()
{
int i,x1;
int tmp,tmp2;
queue<int>q;
tmp=;
dis[]=;
q.push(tmp);
in_queue[]=true;
while(q.size()>)
{
tmp=q.front();
q.pop();
in_queue[tmp]=false;
x1=tmp;
for(i=;i<=n;i++)
{
if(f[x1][i]!=Max && (dis[x1]==HH || dis[x1]+f[x1][i]<dis[i]) )
{
dis[i]=dis[x1]+f[x1][i];
if(in_queue[i]==false)
{
q.push(i);
in_queue[i]=true;
}
}
}
}
}
__int64 dfs(int x)
{
int i;
if(x==) return ;
if(val[x]>) return val[x];
for(i=;i<=n;i++)
{
if(i!=x && f[x][i]!=Max && dis[i]<dis[x])
val[x]+=dfs(i);
}
return val[x];
}
void sc()
{
int i,j;
for(i=;i<=n;i++)
{
printf("\n");
for(j=;j<=n;j++)
printf("%d ",f[i][j]);
}
printf("\n\n");
for(i=;i<=n;i++)
printf("%d ",dis[i]);
}
int main()
{
int i,j,x,y,w;
__int64 k;
while(scanf("%d",&n)>)
{
if(n==)break;
scanf("%d",&m);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
f[i][j]=Max;
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
if(w<f[x][y])
{
f[y][x]=w;
f[x][y]=w;
}
}
for(i=;i<=n;i++)
dis[i]=HH;
memset(val,,sizeof(val));
memset(in_queue,false,sizeof(in_queue));
bfs();
k=dfs();
printf("%I64d\n",k);
// sc();
}
return ;
}

HDU 1142的更多相关文章

  1. hdu 1142 最短路+记忆化

    最短路+记忆化搜索HDU 1142 A Walk Through the Forest链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 > 题意 ...

  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

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...

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

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

  5. hdu 1142 A Walk Through the Forest

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

  6. hdu 1142(迪杰斯特拉+记忆化搜索)

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

  7. hdu 1142(DFS+dijkstra)

    #include<iostream> #include<cstdio> #include<cmath> #include<map> #include&l ...

  8. hdu 1142 用优先队列实现Dijkstra

    之前很认真地看了用优先队列来实现Dijkstra这块,借鉴了小白书上的代码模板后,便拿这道题来试试水了.这道题的大意就是问你从地点1到地点2有多少条满足条件的路径(假设该路径经过 1->...- ...

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

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

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

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

随机推荐

  1. JQuery Mobile - 如何让listview不显示向右的箭头?

    先看一下有向右箭头的截图吧 这个listview第二个项目,就有向右的箭头,如果单纯显示具体数据,没有扩展显示的内容,那么这个向右的箭头就是多余的,在这种情况下,最好是去掉这个向右箭头,程序中已经在第 ...

  2. JavaWeb(HTML +css+js+Servlet....)

    注意 1.不要把函数命名为add(),不然容易和自带的冲突报错 2.是createElement 不要把create中的e写掉了 3.记得是getElementsByTaxName和getElemen ...

  3. 程序猿的日常——SpringMVC系统架构与流程回顾

    web开发经历了很漫长的时间,在国内也快有十几年的时间了.从最开始的进程级到现在的MVC经历了很多的改进和优化,本篇就主要复习了解下Spring MVC相关的知识. 发展历程 第一阶段 CGI进程响应 ...

  4. xss跨站脚本攻击汇总

  5. jvm内存结构(二)(栈的变化,机器指令的格式/执行模式)

    栈的结构: <Java虚拟机原理图解>4.JVM机器指令集 局部变量表: 方法执行时,虚拟机会把字节码中方法数据区的code类型的属性中的局部变量放到栈的局部变量表中. 操作栈: jvm指 ...

  6. absolute

    在需要用到小图标时,可以使用position:absolute,它具有消除float和位置不变特性.使用absolute可以浮现在同级元素的上方.用margin进行精确定位即可,也不必使用top,le ...

  7. 前端 day 30 html 基础一

    前情提要: html基础一 一:html标签入门 1: 1 HTML结构 1)一个html文件有且只有一个html标签.这个就是HTML的根标签.2)一个HTML文件主要由两部分组成:文件头和文件体. ...

  8. linux,软链接配置node,npm全局命令

    sudo ln -s /usr/local/bin/node   /bin/node sudo ln -s /usr/local/bin/npm    /bin/npm 这样配置后,在root下和别的 ...

  9. javaWeb知识点学习(一)

    1.静态页面的传递过程 在静态WEB程序中,客户端使用WEB浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服务器我现在需 ...

  10. Web开发常用在线工具

    http://tool.oschina.net/ 爬去网页工具: http://www.keydatas.com/product