hdoj_2112

HDU Today

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


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

STL map + 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)

const int MAXN = 300;
const int INF = 999999;
int n;
int maps[MAXN][MAXN];
bool visited[MAXN];
int dist[MAXN];

void init()
{
	memset(visited, false, sizeof(visited));
	for (int i = 1; i < MAXN; i++)
	{
		for (int j = 1; j < MAXN; j++)
		{
			if(i == j)
			{
				maps[i][j] = 0;
			}
			else
			{
				maps[i][j] = INF;
			}
		}
	}
}

void Dijkstra(int s, int e) //起点,终点
{
	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];
			}
		}
		if(minNode == e)
		{
			break;
		}
	}
}


int main()
{
	freopen("in.txt", "r", stdin);
	char address[MAXN];
	string str, Start, End;
	map<string, int>Map;
	int t, len, cnt, x, y;
	while (scanf("%d", &t) != EOF)
	{
		if(t == -1)
		{
			break;
		}
		Map.clear();
		cnt = 1;
		cin >> Start >> End;
		init();
		for(int i = 1; i <= t; i++)
		{
			scanf("%s", address);
			str = string(address);
			if(Map.find(str) == Map.end())
			{
				Map[str] = cnt++;
			}
			x = Map[str];
			scanf("%s", address);
			str = string(address);
			if(Map.find(str) == Map.end())
			{
				Map[str] = cnt++;
			}
			y = Map[str];
			scanf("%d", &len);
			if(maps[x][y] > len)
			{
				maps[x][y] = len;
				maps[y][x] = len;
			}
		}
		if(Map.find(Start) == Map.end() || Map.find(End) == Map.end())
		{
			printf("-1\n");
		}
		else
		{
			n = Map.size();
			//cout << n << endl;
			x = Map[Start];
			y = Map[End];
			Dijkstra(x, y);
			if(dist[y] == INF)
			{
				printf("-1\n");
			}
			else
			{
				printf("%d\n", dist[y]);
			}
		}
	}
	return 0;
}


原文地址:https://www.cnblogs.com/lgh1992314/p/5835049.html