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. sublime 把 tab 转成 4 个空格

    Preferences -> Settings-User {    "tab_size":4,    "translate_tabs_to_spaces" ...

  2. postgresql子查询优化(提升子查询)

    问题背景 在开发项目过程中,客户要求使用gbase8s数据库(基于informix),简单的分页页面响应很慢.排查发现分页sql是先查询出数据在外面套一层后再取多少条,如果去掉嵌套的一层,直接获取则很 ...

  3. 《Head First 设计模式》读书笔记

    目录 <Head First 设计模式>读书笔记 创建模式 结构模式 行为模式 用思维导图记录的读书笔记. <Head First 设计模式>读书笔记 模式的分类遵循<设 ...

  4. (转)ldd 查看程序依赖库

    原文:https://blog.csdn.net/u010977122/article/details/52993560?spm=a2c4e.11153940.blogcont551034.8.4f7 ...

  5. java I/O系统总结

    1. InputStream : 从文件.网络.压缩包等中读取 需要的信息到程序中的变量 read();     read(byte []b ); mark(int readlimit); reset ...

  6. Java之集合(六)PriorityQueue

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7299233.html 1.前言 本章介绍队列中的PriorityQueue--优先队列,顾名思义,这是一个可以指 ...

  7. [转]asp.net core中的View Component

    解读ASP.NET 5 & MVC6系列(14):View Component http://www.cnblogs.com/TomXu/p/4496486.html

  8. 【链表】Odd Even Linked List

    题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note ...

  9. 【数组】Best Time to Buy and Sell Stock I/II

    Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...

  10. Eclipse Git 克隆项目的时候出现Internal error; consult Eclipse error log

    在使用git下载代码时偶尔会遇到  Internal error; consult Eclipse error log 这个报错. 简述下个人解决思路: Eclipse 错误日志报错为:org.ecl ...