HDU 1142 a walk through the forest (Shortest Path + DFS)

Source: Internet
Author: User

A walk through the forest Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 5809 accepted submission (s): 2147


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. 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
 

Question: There are several shortest paths. First, use the Dijkstra algorithm to find the shortest path between each vertex and the target vertex, and then search for the number of DIS [v] <dis [u] from the source vertex. Memory-based search is used.

#include"stdio.h"#include"string.h"#include"math.h"#include"iostream"#include"algorithm"using namespace std;#define N 1005const int inf=0x7fffffff;int mark[N],dis[N];int g[N][N],n;void dijkstra(int s){    int i,u,min;    memset(mark,0,sizeof(mark));    for(i=0;i<=n;i++)        dis[i]=g[s][i];    dis[s]=0;    mark[s]=1;    while(1)    {        min=inf;        u=s;        for(i=1;i<=n;i++)        {            if(min>dis[i]&&!mark[i])            {                min=dis[i];                u=i;            }        }        if(u==s)            break;        mark[u]=1;        for(i=1;i<=n;i++)        {            if(g[u][i]!=inf&&dis[i]>dis[u]+g[u][i])                dis[i]=dis[u]+g[u][i];        }    }}int bfs(int u){    if(mark[u])        return mark[u];    int i,cnt=0;    for(i=1;i<=n;i++)    {        if(u==i||g[u][i]==inf)            continue;        if(dis[u]>dis[i])        {            cnt+=bfs(i);        }    }    return mark[u]=cnt;}int main(){    int i,j,u,v,d,m;    while(scanf("%d",&n),n)    {        scanf("%d",&m);        for(i=0;i<=n;i++)        {            for(j=0;j<=n;j++)                g[i][j]=(i==j?0:inf);        }        for(i=0;i<m;i++)        {            scanf("%d%d%d",&u,&v,&d);            g[u][v]=g[v][u]=d;        }        dijkstra(2);        memset(mark,0,sizeof(mark));        mark[2]=1;        int ans=bfs(1);        printf("%d\n",ans);    }    return 0;}


HDU 1142 a walk through the forest (Shortest Path + DFS)

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.