B. A Walk Through the Forest

Time Limit: 1000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

 
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
0

Sample Output

2
4 解题:最短距离+记忆化搜索,找出1到终点2上的所有点,假设A,B两点,如果统计d[A] > D[B]这种路径的条数。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
int mp[maxn][maxn],d[maxn],p[maxn];
int n,m;
bool vis[maxn];
void dij(int src){
int i,j,temp,index;
for(i = ; i <= n; i++)
d[i] = INF;
d[src] = ;
memset(vis,false,sizeof(vis));
for(i = ; i < n; i++){
temp = INF;
for(j = ; j <= n; j++)
if(!vis[j] && d[j] < temp) temp = d[index = j];
vis[index] = true;
for(j = ; j <= n; j++)
if(!vis[j] && d[j] > d[index]+mp[index][j])
d[j] = d[index] + mp[index][j];
}
}
int dfs(int s){
if(p[s]) return p[s];
if(s == ) return ;
int i,sum = ;
for(i = ; i <= n; i++){
if(mp[s][i] < INF && d[s] > d[i]) sum += dfs(i);
}
p[s]+= sum;
return p[s];
}
int main(){
int i,j,u,v,w;
while(scanf("%d",&n),n){
scanf("%d",&m);
for(i = ; i <= n; i++)
for(j = ; j <= n; j++)
mp[i][j] = INF;
for(i = ; i < m; i++){
scanf("%d%d%d",&u,&v,&w);
mp[u][v] = mp[v][u] = w;
}
dij();
memset(p,,sizeof(p));
printf("%d\n",dfs());
}
return ;
}

图论trainning-part-1 B. A Walk Through the Forest的更多相关文章

  1. A Walk Through the Forest[HDU1142]

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

  2. hduoj----1142A 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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  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. UVa 10917 A Walk Through the Forest

    A Walk Through the Forest Time Limit:1000MS  Memory Limit:65536K Total Submit:48 Accepted:15 Descrip ...

  6. hdu_A Walk Through the Forest ——迪杰特斯拉+dfs

    A Walk Through the Forest Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/ ...

  7. HDU1142 A Walk Through the Forest(最短路+DAG)

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

  8. A Walk Through the Forest

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

  9. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

随机推荐

  1. centos6.3下postgresql-9.3安装记录

    Xshell for Xmanager Enterprise 4 (Build 0186) Copyright (c) 2002-2011 NetSarang Computer, Inc. All r ...

  2. ARM指令解析

    今天我来总结一下arm指令的学习,今天我不会对所有的arm指令进行一一的解析,在这里希望大家去看arm汇编手册,这个手册的中文版我放在了http://download.csdn.net/detail/ ...

  3. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} 解决方法

    Tomcat启动时出现红色警告内容 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'sour ...

  4. windows Git的安装和使用

    一.本人是根据廖雪峰大神的个人官网学习的git,并有感而发以做笔记的方式写下这篇博客,希望可以帮助到自己和其他人,廖雪峰个人官网http://www.liaoxuefeng.com/ 二.声明我的电脑 ...

  5. vue+element ui项目总结点(三)富文本编辑器 vue-wangeditor

    1.参考 https://www.npmjs.com/package/vue-wangeditor 使用该富文本编辑器 <template> <div class="egi ...

  6. C++和ASM文件的互相调用

    1. C++调用ASM中的函数,需要在ASM源文件中指定.model flat, c 就是指定以C的形式编译,然后在头文件中用EXTERN_C声明这个头文件就可以了 2. ASM中调用C++函数,需要 ...

  7. Fixed table

    废话不多说,直接代码. <!DOCTYPE> <html> <head> <meta charset="utf-8"/> <s ...

  8. 目后佐道IT教育:师资团队

    高端技术顾问 1. leepoor 拥有12年的Web开发和架构经验,在阿里巴巴担任高级架构师,参与阿里巴巴基础技术平台开发和www.alibaba.com架构设计.擅长大型网站技术架构,工作中经常使 ...

  9. 迅为IMX6Q开发板在道路交通信号控制系统解决方案中的应用

    智能交通综合管控平台是为交通指挥系统服务的统一信息平台,以信息技术为主导,以计算机通信网络和智能化指挥控制管理为基础,建成集高新技术应用为一体的智能化指挥调度集成平台,实现信息交换与共享.快速反应决策 ...

  10. UVA10917 A walk trough the Forest (最短路,dp)

    求出家到其他点的最短路径,题目的条件变成了u->v不是回头路等价于d[u]>d[v]. 然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v]). #includ ...