http://acm.hdu.edu.cn/showproblem.php?pid=1142

来源:互联网 发布:软件开发行业前景 编辑:程序博客网 时间:2024/05/22 19:48

A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3848    Accepted Submission(s): 1408


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

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<iostream>#include<stdio.h>using namespace std;const int maxInt=0xffff;int graph[9999][9999];int n,m;int dist[maxInt];bool visit[maxInt];int path[maxInt];void dijkstra(int source){    for(int i=1;i<=n;i++)    {        dist[i]=maxInt;        visit[i]=false;        path[i]=0;    }    dist[source]=0;    for(int i=1;i<n;i++)    {        int u=0;        int min=maxInt;        for(int j=1;j<=n;j++)        {            if(min>dist[j]&&!visit[j])            {                u=j;                min=dist[j];            }        }        visit[u]=true;        for(int k=1;k<=n;k++)        {            if(graph[u][k]!=maxInt&&!visit[k])            {                if(dist[k]>dist[u]+graph[u][k])                {                    dist[k]=dist[u]+graph[u][k];                }            }        }    }}int DFS(int s,int n){    if(s==2)    return 1;    int sum=0;    for(int i=1;i<=n;i++)    {        if(graph[s][i]!=maxInt&&dist[s]>dist[i])        {            if(path[i]) sum=sum+path[i];            else            sum=sum+DFS(i,n);        }    }    path[s]=sum;    return path[s];}int main(){    int a,b,c;    while(scanf("%d",&n))    {        if(n==0)        break;        scanf("%d",&m);        for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)        graph[i][j]=maxInt;        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&a,&b,&c);            graph[a][b]=graph[b][a]=c;        }        dijkstra(2);        printf("%d\n",DFS(1,n));    }    return 0;}