【最短路+模板题】杭电 hdu 2112 HDU Today

/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
	Copyright (c) 2011 panyanyany All rights reserved.

	URL   : http://acm.hdu.edu.cn/showproblem.php?pid=2112
	Name  : 2112 HDU Today

	Date  : Friday, January 13, 2012
	Time Stage : 

	Result: 
5250873	2012-01-13 10:05:57	Accepted	2112
2703MS	416K	1963 B
C++	pyy

5250866	2012-01-13 10:03:43	Wrong Answer	2112
2718MS	416K	1934 B
C++	pyy


Test Data :

Review :
这是个无向图,要判断有无重边。基本上属于模板题。
//----------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std ;

#define INF		0x3f3f3f3f
#define MAXN	202

#define min(x, y)	((x) < (y) ? (x) : (y))

bool	used[MAXN] ;

int		n, iAreaCnt ;
int		map[MAXN][MAXN], dist[MAXN] ;

string	id[MAXN] ;

int getId (string str)
{
	int i ;

	for (i = 0 ; i < iAreaCnt ; ++i)
		if (str == id[i])
			return i ;

	id[iAreaCnt] = str ;

	return iAreaCnt++ ;
}

int dijkstra (const int start, const int end)
{
	int i, j ;
	int iMinPath, MinPath ;

	memset (used, 0, sizeof (used)) ;
	for (i = 0 ; i < iAreaCnt ; ++i)
	{
		dist[i] = map[start][i] ;
	}

	dist[start] = 0 ;

	for (i = 0 ; i < iAreaCnt ; ++i)
	{
		iMinPath = 0 ;
		MinPath = INF ;
		for (j = 0 ; j < iAreaCnt ; ++j)
		{
			if (!used[j] && dist[j] < MinPath)
			{
				iMinPath = j ;
				MinPath = dist[j] ;
			}
		}
		used[iMinPath] = true ;
		for (j = 0 ; j < iAreaCnt ; ++j)
			if (!used[j] && dist[iMinPath] + map[iMinPath][j] < dist[j])
				dist[j] = dist[iMinPath] + map[iMinPath][j] ;
	}
	if (dist[end] == INF)
		return -1 ;
	return dist[end] ;
}

int main (void)
{
	int		i, j ;
	int		start, end, x, y, c ;
	string	a, b ;

	while (scanf ("%d", &n), n != -1)
	{
		iAreaCnt = 0 ;

		cin >> a >> b ;
		start = getId (a) ;
		end = getId (b) ;

		memset (map, INF, sizeof (map)) ;

		for (i = 0 ; i < n ; ++i)
		{
			cin >> a >> b >> c ;
			x = getId (a) ;
			y = getId (b) ;

			map[x][y] = map[y][x] = min (map[x][y], c) ;
		}

		printf ("%d\n", dijkstra (start, end)) ;
	}
	return 0 ;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的安徒生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值