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. Socket编程概念

    一.网路套接字 在通信过程中,套接字是成对存在的,该套接字内部借助两个缓冲区实现 二.网络字序 1.存储方式 大端法(网络):高位存低位,低位存高位 小端法(本地):高位存高位,低位存低位 2.网络字 ...

  2. C/C++ 语言 Hello world

    #include <stdio.h> void main() { int x,i; ; scanf("%d",&x); if(x>y) printf(&q ...

  3. 一个Unix内核级别漏洞(一)

    翻译原创稿件,prison整理翻译,首发ichunqiu,原地址:http://lsd-pl.net/kernelvuln.pdf 这是一篇关于Unix内核级别漏洞的paper,由某团队发布在一次黑客 ...

  4. SELECT 三级联动 [转]

    <!DOCTYPE html> <html> <head> <meta charset=gbk /> <title>selectList&l ...

  5. LruCache源码分析

    LRU(Least Recently Used)是一种很常用的资源调度策略,与20/80原则契合,在资源达到上限时倾向保留最近经常访问的资源对象. Android中基于LRU实现了缓存对象,即LruC ...

  6. (转)MYSQL线程池总结(一)

    MYSQL线程池总结(一)  原文:http://www.cnblogs.com/cchust/p/4510039.html 线程池是Mysql5.6的一个核心功能,对于服务器应用而言,无论是web应 ...

  7. PHP中 LFI Local File Include,本地文件包 漏洞

    在allow_url_include=On就是远程文件包含了,假设这里为off,那就只能本地包含了. 1.       包含上传文件 只要目标服务器支持上传,不管是jpg,txt,gif等都可以,在其 ...

  8. docker - 修改镜像/容器文件或者 "Docker root dir" 的在宿主机上的存储位置

    背景 之前在使用docker的时候,由于启动container的时候用的是默认的mount(路径为 /var/lib/docker),这个目录对应的硬盘空间有限,只有200G左右.现在随着程序运行,有 ...

  9. slq 修改表结构

    1.增加列: alter table tableName add columnName varchar(30) 2.修改列类型: alter table tableName alter column  ...

  10. 在k8s中搭建可解析hostname的DNS服务

    2016-01-25更新 上篇文章总结k8s中搭建hbase时,遇到Pod中hostname的DNS解析问题,本篇将通过修改kube2sky源码来解决这个问题. 1 前言 kube2sky在Githu ...