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. C#oracle备份和还原

    最近公司的oracle备份工具不好使了,原来是公司的人用VB写的,由于我是主攻C#的,所以想着自己来写一个C#版本的oracle备份和还原工具. 一开始,我按照原来的设计思路来进行编写,想在pluss ...

  2. 绘制复杂的原理图元件和pcb封装库用于cadence(一)

    绘制TI公司的TPS53319电源芯片封装 由于产品设计需要大电流电源供电,选用TI公司TPS53319电源芯片通过cadence软件进行电路设计,但是TI公司所提供的封装格式为CAD File(.b ...

  3. go查询mysql到list<map>

    func selects() { db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test?cha ...

  4. Java编程基础-运算符

    Java中的运算符大致分为:算术运算符.赋值运算符.关系运算符.逻辑运算符和位运算符五类. (1).算术运算符:+  -  *  /  %  ++  -- (2).赋值运算符:=  +=  -=  * ...

  5. 16年毕业的前端er在杭州求职ing

    来杭州也有一两个星期了,这个周末下雨,是在没地去,还是习惯性的打开电脑逛技术论坛,想想也是好久没有更新博文了... 背景 因为曾经看过一篇文章面试分享:一年经验初探阿里巴巴前端社招所以来杭州也是带有目 ...

  6. 涉及到弹出层的opacity样式问题

    最近遇到一个弹出层在Chrome中重复的问题,观察发现是opacity引起的以下是代码及现象<!DOCTYPE html><html> <head>         ...

  7. COGS 615. 韩国明星

    [问题描述] 在LazyCat同学的影响下,Roby同学开始听韩国的音乐,并且越来越喜欢H.o.T,尤其喜欢安七炫和Tony,可是,爱学习爱思考的Roby同学想,如果以后喜欢的韩星越来越多怎么办呢?R ...

  8. (十二)mybatis之动态代理

    mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...

  9. Linux系统里让vim支持markdown格式的语法高亮

    Markdown是深受程序员喜爱的一个文件格式. 然而Linux里默认的vim设置,并不支持markdown格式的语法高亮显示. 下面就来介绍如何设置使得markdown格式的文件在vim里也能享有语 ...

  10. 使用python批量导入txt导入excel表格(公司电脑设备ip和人员统计)

    #!/bin/env python # -*- encoding: utf- -*- import datetime import time import os import sys import x ...