Link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1142
Question:
A walk through the forest
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3689 accepted submission (s): 1340
Problem descriptionjimmy 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.
Inputinput 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.
Outputfor 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
Analysis and Summary:
This is really a wave of twists and turns, and WAF has been doing more than 30 times.
1. The meaning of the question is incorrect. A paragraph in the question is not well understood:
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.
This sentence means that for two points A and B, if the shortest distance from point B to the home is smaller than the shortest distance from point A to the home, then a can reach B.
I understand this question as to the number of different shortest paths from the Office to the home.
2. After the above error is solved, you can find the shortest path of each point arriving at point 2 (home), and then find the number of paths to the home based on the shortest path of each point.
If direct search times out, you need to perform a memory-based search.
3. because of my carelessness, because my program has two arrays named D and DP, these two arrays are confused when writing the memory words for search, as a result, the reason was lost after more than 10 times.
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<utility>#include<string>#include<map>using namespace std;typedef int Type;typedef pair<Type,int>pii;const Type INF = 0x7fffffff;const int VN = 1005;const int EN = VN*VN;int n,m,size;int head[VN];Type d[VN];bool G[VN][VN];bool vis[VN];int cnt;int dp[VN];struct Edge{ int v,next; Type w;}E[EN];void init(){ size=0; cnt=0; memset(G, 0, sizeof(G)); memset(head, -1, sizeof(head)); memset(dp, -1, sizeof(dp));}void addEdge(int u,int v,Type w){ E[size].v=v; E[size].w=w; E[size].next=head[u]; head[u]=size++;}void Dijkstra(int src){ for(int i=1; i<=n; ++i) d[i]=INF; d[src] = 0; priority_queue<pii,vector<pii>,greater<pii> >q; q.push(make_pair(d[src],src)); while(!q.empty()){ pii x = q.top(); q.pop(); int u = x.second; if(d[u] != x.first)continue; for(int e=head[u]; e!=-1; e=E[e].next){ Type tmp=d[u]+E[e].w; if(d[E[e].v] > tmp){ d[E[e].v] = tmp; q.push(make_pair(tmp, E[e].v)); } } }}int dfs(int u){ if(dp[u]!=-1) return dp[u]; if(u==2) return 1; int cnt=0; for(int v=1; v<=n; ++v)if(d[v]<d[u] && G[u][v]){ cnt += dfs(v); } dp[u] = cnt; return dp[u];}int main(){ int u,v; Type w; while(~scanf("%d",&n)&n){ scanf("%d",&m); init(); for(int i=0; i<m; ++i){ scanf("%d%d%d",&u,&v,&w); G[u][v] = G[v][u] = 1; addEdge(u,v,w); addEdge(v,u,w); } Dijkstra(2); printf("%d\n", dfs(1)); } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)