HDU 1142 A Walk Through the Forest (digkstra+ memory Search)

Source: Internet
Author: User

A Walk Through the ForestTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7322 Accepted Submission (s): 2685


Problem Descriptionjimmy experiences a lot of stress at work these days, especially since he accident made working diffic Ult. To relax after a hard day, he likes to walk home. To make things even nicer, he office is on one side of a forest, and he house was on the other. A Nice walk through the forest, seeing the birds and chipmunks are 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 is always takes a path to make progress towards his house. He considers taking a path from a to B to being progress if there exists a route from B to his home that's 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 had numbered each intersection or joining of paths starting with 1. His office is numbered 1, and he house is numbered 2. The first line of all 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 o F Length D between intersection A and a different intersection B. Jimmy may walk a path any direction he chooses. There is at the most one of the path between any pair of intersections.

Outputfor each test case, output a single integer indicating the number of different routes through the forest. Assume that is 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

Sourceuniversity of Waterloo Local Contest 2005.09.24
Recommendeddy | We have carefully selected several similar problems for you:1217 1385 1162 1596 2112
Statistic | Submit | Discuss | Note

There is no English class four slag slag really can not do this problem.

Understand the number of shortest paths. That's the main thing. He considers taking a path from a to B to being progress if there exists a route from B to his home   Er than any possible route from A. It means that if a-B has a path and I want to go from a to a, I have the condition that the distance from a to 2 is greater than the distance from B to 2.

Ideas:

The 1.digkstra algorithm is used to find the shortest distance between points to 2-1 and the shortest distance from other points to 2.

2. Memory search starting from 1 find the number of paths that match the criteria

#include <stdio.h> #include <vector> #include <string.h> #include <queue> #include < Algorithm>using namespace Std;int min_path;int min_count;//Storage current point to 2 shortest path int dis[1005];//memory path number of storage paths int dp[1005]; int N;//digkstra algorithm and memory search to determine if you have traversed some point bool vis[1005];struct node{int pos;int cost;bool friend operator< (node X,node y) { Return x.cost>y.cost;}};/ /Storage Edge Vector<node>map[1005];int Digkstra (int x) {Priority_queue<node>s;memset (vis,false,sizeof (VIS)); memset (dis,100,sizeof (dis)); node Temp,temp1;temp.pos=x;temp.cost=0;s.push (temp); while (!s.empty ()) {temp=temp1= S.top (); S.pop (); Vis[temp.pos]=true;dis[temp.pos]=min (dis[temp.pos],temp.cost); if (temp.pos==1) return temp.cost; for (int i=0;i<map[temp.pos].size (); i++) {int x=map[temp.pos][i].pos;int y=map[temp.pos][i].cost;if (!vis[x]) { Temp.pos=x;temp.cost+=y;s.push (temp);} TEMP=TEMP1;}} return-1;} Memory Search int dfs (int pos,int sum) {if (Dp[pos]) return dp[pos];if (pos==2) return 1;int result=0;for (int i=0;i<map[pos]. Size ();i++) {int X=map[pos][i].pos;int y=map[pos][i].cost;//pos->x has a route and satisfies the shortest path that POS to 2 is greater than X to 2 if (!vis[x]&&dis[ Pos]>dis[x]) {Vis[x]=true;result+=dfs (x,sum+y); vis[x]=false;}} Dp[pos]=result;return Dp[pos];} int main () {while (~SCANF ("%d", &n) &&n) {int m;scanf ("%d", &m), memset (map,0,sizeof (map)), and for (int i=0;i <m;i++) {int a,b,x;scanf ("%d%d%d", &a,&b,&x); node Temp;temp.pos=b;temp.cost=x;map[a].push_back (temp ); Temp.pos=a;map[b].push_back (temp);} Min_path=digkstra (2); Min_count=0;memset (vis,false,sizeof (Vis)); Memset (Dp,0,sizeof (DP)); VIS[1]=TRUE;PRINTF ("%d\ N ", DFS (1,0));} return 0;}



HDU 1142 A Walk Through the Forest (digkstra+ memory Search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.