zoukankan      html  css  js  c++  java
  • hdoj_Choose the best route

    Choose the best route

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4888    Accepted Submission(s): 1547


    Problem Description
    One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
     

    Input
    There are several test cases. 
    Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
    Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
    Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
     

    Output
    The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
     

    Sample Input
    5 8 5 1 2 2 1 5 3 1 3 4 2 4 7 2 5 6 2 3 5 3 5 1 4 5 1 2 2 3 4 3 4 1 2 3 1 3 4 2 3 2 1 1
     

    Sample Output
    1 -1
    注意题目是有向图,将起点终点逆置,就能用dijkstra了

    #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)
    
    #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];
    
    void Dijkstra(int s)
    {
    	int i, j;
    	int minValue, minNode;
    
    	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 main()
    {
    	freopen("in.txt", "r", stdin);
    	int m, s, p, q, t, x, w, ans;
    	while (scanf("%d %d %d", &n, &m, &s) != EOF)
    	{
    		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;
    				}
    			}
    		}
    		while (m--)
    		{
    			scanf("%d %d %d", &p, &q, &t);
    			if(maps[q][p] > t)
    			{
    				maps[q][p] = t;
    			}
    		}
    		memset(visited, false, sizeof(visited));
    		Dijkstra(s);
    		ans = INF;
    		scanf("%d", &w);
    		while (w--)
    		{
    
    			scanf("%d", &x);
    			if(ans > dist[x])
    			{
    				ans = dist[x];
    			}
    		}
    		if(ans == INF)
    		{
    			printf("-1\n");
    		}
    		else
    		{
    			printf("%d\n", ans);
    		}
    	}
    	return 0;
    }



  • 相关阅读:
    .NET实现图片切割效果(带拖放、缩放效果)
    合成艺术字二 :使用的透明类以及所用的颜色选择器JS(完整事列源码)
    Ajax.net中的Web服务
    IE6和IE7共存方法 (转)
    sql取不重复多字段
    CSS完美兼容IE6/IE7/FF的通用方法
    发布事件.net框架程序设计
    const和readonly
    CSS网站实用技巧:wordwrap同wordbreak的区别
    FCKeditor 2.1.1在ASP.NET中的设置和使用(转)
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835041.html
Copyright © 2011-2022 走看看