A Walk Through the Forest(最短路径+DFS)
来源:互联网 发布:unity3d做2d游戏 编辑:程序博客网 时间:2024/05/19 13:58
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123 Accepted Submission(s): 59Problem 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.
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 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
Sample Output
24
#include <stdio.h>#include <iostream>#include <queue>#include <vector>#include <cmath>#include <algorithm>#include <string.h>#define INF 0x3fffffff#define MAX_N 5005#define MAX_E 5005#define MAX_V 50005using namespace std;typedef long long ll;typedef pair<int, int> pp;struct edge { int to; int w;};edge e;int u, v, w;int V, E;int N, M, R;vector<edge> G[MAX_V];int d[MAX_V];int ans[MAX_V];int vis[MAX_V];void dijkstra(int s) { priority_queue<pp, vector<pp>, greater<pp> > q; fill(d, d+V+1, INF); d[s] = 0; q.push(pp(0, s)); while (q.size()) { pp p = q.top(); q.pop(); int v = p.second; int w = p.first; if (d[v] < w) continue; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; if (d[e.to] > d[v] + e.w) { d[e.to] = d[v] + e.w; q.push(pp(d[e.to], e.to)); } } }}int dfs(int v) { int sum = 0; if (ans[v] != -1) return ans[v]; // 1到达2 if (v == 2) return 1; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; // 如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路 if (d[v] > d[ e.to ]) { sum += dfs(e.to); } } return ans[v] = sum;}int main(void){ // freopen("in.txt", "r", stdin); while (~scanf("%d%d", &V, &E)) { if (V == 0) break; memset(G, 0, sizeof(G)); for (int i = 0; i < E; i++) { scanf("%d%d%d", &u, &v, &e.w); e.to = v; G[u].push_back(e); e.to = u; G[v].push_back(e); } // 得到顶点2到其他顶点的最短距离 dijkstra(2); memset(ans, -1, sizeof(ans)); printf("%d\n", dfs(1)); } return 0;}
0 0
- A Walk Through the Forest(最短路径+DFS)
- A Walk Through the Forest(最短路径+深搜)
- hdu 1142 A Walk Through the Forest(最短路径+dfs)
- HDOJ 1142 A Walk Through the Forest(最短路径)
- HDU-1142 A Walk Through the Forest(最短路径+记忆化搜索)
- A Walk Through the Forest(最短路+dfs)
- A Walk Through the Forest(hdu1142,最短路+dfs)
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- hdu1142-A Walk Through the Forest-最短路+dfs
- HDU 1142 A Walk Through the Forest 最短路+DFS
- hdu 1142 A Walk Through the Forest (最短路+dfs )
- HDU 1142 A Walk Through the Forest (搜索-DFS)
- 杭电1142 A Walk Through the Forest (最短路+DFS+dp)
- 【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest
- A Walk Through the Forest 最短路+记忆化搜索DFS
- hdu1142 A Walk Through the Forest 最短路和记忆式搜索dijstra+dfs
- hdu 1142 A Walk Through the Forest【Dijkstra+dfs求最短路条数】
- HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)
- hdu 2046 骨牌铺方格(递推 斐波那契数列)
- RedHat linux 安装 Oppenoffice 方法
- Spring mvc Interceptor 解决Session超时配置流程
- mysql和sqlserver在变量上的区别
- FunDA(5)- Reactive Streams:Play with Iteratees
- A Walk Through the Forest(最短路径+DFS)
- SAP数据更新
- React×Redux——react-redux库connect()方法与Provider组件
- 读书笔记-捌-《创业维艰》
- 最小生成树(prime算法、kruskal算法) 和 最短路径算法(floyd、dijkstra)
- 算法提高 贪吃的大嘴
- CENTOS: Docker网络突然断了的一种情况
- 正则表达式
- opencv读取图像