【最短路+模板题】杭电 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 ;
}


 

你可能感兴趣的:(【最短路+模板题】杭电 hdu 2112 HDU Today)