• poj_2662A Walk Through the Forest


    A Walk Through the Forest
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2273   Accepted: 831

    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
    略过了这一段话,很自然的就按照求有多少最短路径了。

    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. 


    当我们选择B的时候,一定是B到终点的距离小于A到终点的距离。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    
    const int MAXN = 1005;
    const int INF = 999999;
    int n;
    int maps[MAXN][MAXN];
    bool visited[MAXN];
    int dist[MAXN];
    int dp[MAXN];
    
    void Dijkstra(int s)
    {
    	int i, j;
    	int minValue, minNode;
    
    	memset(visited, false, sizeof(visited));  
    	dist[s] = 0;
    	visited[s] = true;
    	for (i = 1; i <= n; i++)
    	{
    		dist[i] = maps[s][i];
    	}
    	for (i = 1; i <= n; i++)
    	{
    		minValue = INF;
    		minNode = 0;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && minValue > dist[j])
    			{
    				minNode = j;
    				minValue = dist[j];
    			}
    		}
    		if(minNode == 0)
    		{
    			break;
    		}
    		visited[minNode] = true;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && maps[minNode][j] != INF && dist[j] > dist[minNode] + maps[minNode][j])
    			{
    				dist[j] = dist[minNode] + maps[minNode][j];
    			}
    		}
    	}
    }
    
    int dfs(int v)
    {
    	if (dp[v] != -1)
    	{
    		return dp[v];
    	}
    	if(v == 2)
    	{
    		return 1;
    	}
    	dp[v] = 0;
    	for (int i = 1; i <= n; i++)
    	{
    		if(maps[v][i] != INF && dist[i] < dist[v])
    		{
    			dp[v] += dfs(i);
    		}
    	}
    	return dp[v];
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int m, a, b, d;
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		for (int i = 1; i <= n; i++)  
    		{  
    			for (int j = 1; j <= n; j++)  
    			{
    				if(i == j)
    				{
    					maps[i][j] = 0;
    				}
    				else
    				{
    					maps[i][j] = INF;  
    				}
    			}  
    		}  
    		scanf("%d", &m);
    		while (m--)
    		{
    			scanf("%d %d %d", &a, &b, &d);
    			if(maps[a][b] > d)
    			{
    				maps[a][b] = d;
    				maps[b][a] = d;
    			}
    		}
    		Dijkstra(2);
    		for (int i = 1; i <= n; i++)
    		{
    			cout << dist[i] << " ";
    		}
    		cout << endl;
    		memset(dp, -1, sizeof(dp));
    		cout << dfs(1) << endl;
    	}
    	return 0;
    }


  • 相关阅读:
    Maven版本管理
    ArrayList集合实现RandomAccess接口有何作用?为何LinkedList集合却没实现这接口
    java常用集合框架关系
    重写equals和hashCode
    项目的继承和聚合详解
    Installation Manager1.8安装
    关于java按位操作运算
    正数负数的二进制表示
    springboot问题排解
    int和Integer有什么区别
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835057.html
Copyright © 2020-2023  润新知