zoukankan      html  css  js  c++  java
  • HDU 2112 HDU Today(Dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112


    HDU Today

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 14527    Accepted Submission(s): 3412


    Problem Description
    经过锦囊相助,海东集团最终度过了危机。从此。HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候。XHD夫妇也退居了二线。并在风景秀美的诸暨市浬浦镇陶姚村买了个房子。開始安度晚年了。


    这样住了一段时间。徐总对当地的交通还是不太了解。

    有时非常郁闷,想去一个地方又不知道应该乘什么公交车。在什么地方转车,在什么地方下车(事实上徐总自己有车,却一定要与民同乐,这就是徐总的性格)。


    徐总常常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
    请帮助他用最短的时间到达目的地(如果每一路公交车都仅仅在起点站和终点站停。并且随时都会开)。

     
    Input
    输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);
    第二行有徐总的所在地start,他的目的地end;
    接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每一个地名是一个长度不超过30的字符串)。


    note:一组数据中地名数不会超过150个。


    假设N==-1。表示输入结束。

     
    Output
    假设徐总能到达目的地,输出最短的时间。否则,输出“-1”。


     
    Sample Input
    6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
     
    Sample Output
    50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 尽管偶尔会迷路,可是由于有了你的帮助 **和**从此还是过上了幸福的生活。

    ――全剧终――

     
    Author
    lgx
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2066 1142 1385 1690 2722 


    代码例如以下:
    #pragma warning (disable:4786)
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <cstdlib>
    #include <climits>
    #include <ctype.h>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <deque>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define PI acos(-1.0)
    #define INF 0x3fffffff
    //typedef long long LL;
    //typedef __int64 LL;
    #define MAXN 10017
    #define M 32
    int mat[MAXN][MAXN];
    int n;
    #define MAX 150
    map<string,int>m;
    void init()
    {
    	for(int i = 0; i <= MAX; i++)
    	{
    		for(int j = 0; j <= MAX; j++)
    		{
    			if(i == j)
    				mat[i][j] = 0;
    			else
    				mat[i][j] = INF;
    		}
    	}
    	m.clear();
    }
    
    int dijkstra (int s,int f)
    {
    	//s为起点, f:为终点
    	int dis[MAXN];//记录到随意点的最短距离
    	int mark[MAXN];//记录被选中的结点 
    	int i,j,k = 0;
        for(i = 0; i <= MAX; i++)
        {
    		mark[i] = 0;//初始化全部结点,每一个结点都没有被选中 
            dis[i] = INF;
        }
        mark[s] = 1;//start结点被选中 
        dis[s] = 0;//将start结点的的距离设置为0 
        int min ;//设置最短的距离。 
        for(i = 1; i <= MAX; i++)
        {
    		k = 1;
            min = INF;
            for(j = 1; j <= MAX; j++)
            {
                if(mark[j] == 0  && dis[j] < min)//未被选中的结点中,距离最短的被选中 
                {
                    min = dis[j] ;
                    k = j;
                }
            }
            mark[k] = 1;//标记为被选中 
            for(j = 1; j <= MAX; j++)
            {
                if( mark[j] == 0  && (dis[j] > (dis[k] + mat[k][j])))//改动剩余结点的最短距离 
                {
                    dis[j] = dis[k] + mat[k][j];
                }
            }
        }
        return dis[f];    
    } 
    
    int main()
    {
    	int i;
    	char s[M], e[M], a[M], b[M];
    	int v;
    	while(~scanf("%d",&n))
    	{
    		init();
    		if(n == -1)
    			break;
    		scanf("%s%s",s,e);
    		m[s] = 1;
    		if(!m[e])
    			m[e] = 2;
    		int c = 3;
    		for(i = 0; i < n; i++)
    		{
    			scanf("%s%s%d",a,b,&v);
    			if(!m[a])
    				m[a] = c++;
    			if(!m[b])
    				m[b] = c++;
    			if(v < mat[m[a]][m[b]])
    			{
    				mat[m[a]][m[b]] = v;
    				mat[m[b]][m[a]] = v;
    			}
    		}
    		int ans = dijkstra(m[s],m[e]);
    		if(ans == INF)
    			printf("-1
    ");
    		else
    			printf("%d
    ",ans);
    	}
    	return 0;
    }




  • 相关阅读:
    mybatis源码追踪2——将结果集映射为map
    Mybatis的cache
    mybatis拦截器
    mybatis中单个参数的引用
    mybatis源码追踪1——Mapper方法用法解析
    win8 下 intellij idea 13 中文输入覆盖的问题
    firebug中html显示为灰色的原因总结
    extjs4.0以上添加多行工具栏的方法
    去除eclipse的validating
    An interview question from MicroStrategy
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5176156.html
Copyright © 2011-2022 走看看