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 ;
}

最新文章

  1. [手机取证] “神器”IP-BOX的一些问题
  2. OpenMP之求和(用section分块完成)
  3. centos 下 安装zookpeer
  4. HTML5-WebSocket技术学习(2)
  5. linux下 链接 sqlserver数据库 驱动的安装
  6. pthread_rwlock_t读写锁函数说明
  7. CC++初学者编程教程(5) 安装codeblocks软件开发环境
  8. Java 获取 文件md5校验码
  9. 关于 MVCC 的基础
  10. POJ 2484 A Funny Game(找规律)
  11. Git协作流程
  12. MongoDB:数据库介绍与基础操作
  13. mybatis入门配置和调试
  14. 如何设计出优秀的Restful API?
  15. python,接口自动化有几大类
  16. redis使用规范文档 20170522版
  17. Windows samba history
  18. 【Python基础】json.dumps()和json.loads()、json.dump()和json.load()的区分
  19. ios 中sqlite的用法
  20. HTML中块级元素与内联元素有什么区别 ?

热门文章

  1. CAD 安装时出现.net frameword 3.5安装不上的问题
  2. 前端之CSS布局模型
  3. vue从入门到开发--4--处理http请求
  4. 解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法
  5. SQLServer同一实例下事务操作
  6. Install your Application into RaspberryPi2 and automatically start up
  7. 迅为iMX6Q/PLUS开发板烧写设备树内核 Qt 系统
  8. CAD交互绘制圆形批注(网页版)
  9. css3中animation属性animation-timing-function知识点以及其属性值steps()
  10. Java中的线程--并发库中的集合