HD1142 穿越丛林

A Walk Through the Forest

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


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 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0
 

Sample Output
   
   
   
   
2 4
 

Source
University of Waterloo Local Contest 2005.09.24 

#include <iostream>
#include<vector>
#include<queue>
#include <string>
#include <algorithm>
using namespace std;
#define maxn  1000 +10
#define maxdis 1000000 + 1

int graph[maxn][maxn];
int dist[maxn];
int res[maxn];
bool isShortest[maxn];
//N个节点,M条边
int N,M;

int dijkstra()
{
	//初始化
	for( int i = 1 ; i<= N;i++)
	{
			if(  graph[2][i] == -1 )
			{
				dist[i] = maxdis;
			}
			else
			{
				dist[i] = graph[2][i];
			}
			memset(isShortest,false,sizeof(isShortest));
	}
	isShortest[2] = true;
	dist[2] = 0;

	for( int i = 1; i< N;i++)
	{
		//求出最短路径长度,更新集合
		int minv = maxdis;	
		int point = 0;
		for( int i = 1; i<= N ; i++)
		{
			if( isShortest [ i] == false && dist[ i] < minv)
			{
				minv = dist[i];
				point = i;
			}
		}
		isShortest [point] = true;
		
		//通过新的最短路径,修改dist
		for( int j = 1 ;j <=N ;j++)
		{
			if( isShortest[j] == false && dist[j] > dist[point] + graph[point][j] && graph[point][j]!= -1 )
			{
				dist[j] =  dist[point] + graph[point][j];
			}
		}
	}
	//结束
	return 1;
}

void calcuPath( int &cnt,int cur,int dest)
{
	//递归太慢
	/*if( cur == dest)
	{
		cnt++;
		return;
	}
	for( int i = 1; i<= N;i++)
	{
		if( graph[cur][i] > 0 && dist[cur] > dist[i] )
		{
			 calcuPath(cnt,i,dest);
		}
	}*/

	//每个节点查找可能不止一次,全连通图复杂度有可能远大于n^2, 到 n! 
	queue<int > q;
	q.push(1);
	cnt = 1;
	while ( !q.empty())
	{
		int f = q.front();
		q.pop();
		int c =-1;
		for( int i = 1; i<= N;i++)
		{
			if( graph[f][i] > 0 && dist[f] > dist[i] )
			{
				c++;
				if( i!= 2) q.push(i);
			}
		}
		cnt+=c;
	}
}

/**记忆化搜索*/  
//记录每个点的路径数
int solve( int start,int dest ){  
	//终止条件
    if(res[start]) return res[start];//该点已经走过,返回:过该点有多少条路   
    if(start==dest)return 1;//找到终点,返回1(1条路)   

	//求解子问题并递归
    for( int i=1;  i<=N; i++)  
        if(graph[start][i]!=-1 && dist[start]>dist[i])  
            res[start]+=solve(i,2);  
    return res[start];//返回:该点一共可以有多少条路   
}  
int main()
{
	//freopen("input.txt","r",stdin);
	while(cin>>N>>M && N!= 0)
	{
		int a,b,d;
		int cnt = 0;
		memset(graph,-1,sizeof(graph));
		memset(res,0,sizeof(res));
		for( int i = 0;i< M ;i++)
		{
			cin>>a>>b>>d;
			graph[a][b] = d;
			graph[b][a] = d;
		}
		dijkstra();
		//calcuPath(cnt,1,2);
		//cout<<cnt<<endl;
		cout<<solve(1,2)<<endl;
	}

	return 0;
}


你可能感兴趣的:(HD)