杭电 hdu 1548 A strange lift


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

URL   : http://acm.hdu.edu.cn/showproblem.php?pid=1548
Name  : 1548 A strange lift

Date  : Thursday, August 25, 2011
Time Stage : more than half an hour

Result:
4492153	2011-08-25 19:42:55	Accepted	1548
31MS	400K	1680 B
C++	pyy


Test Data:


Review:
单向的最短路(bfs也可以做),只是在制图的时候要注意,其他都跟普通的最短路一样了
//----------------------------------------------------------------------------*/

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

#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

#define infinity    0x0f0f0f0f
#define minus_inf    0x80808080

#define MAXSIZE	209

#define SafeAccess(x) (0 < (x) && (x) <= n)

int n, s, e ;
int map[MAXSIZE][MAXSIZE], used[MAXSIZE], dp[MAXSIZE] ;

void dijkstra ()
{
	int i, j, iMinPath, MinPath ;
	memset (used, 0, sizeof (used)) ;
	for (i = 1 ; i <= n ; ++i)
		dp[i] = map[s][i] ;
	dp[s] = 0 ;
	
	for (i = 1 ; i <= n ; ++i)
	{
		MinPath = infinity ;
		iMinPath = 0 ;
		for (j = 1 ; j <= n ; ++j)
			if (!used[j] && dp[j] < MinPath)
			{
				iMinPath = j ;
				MinPath = dp[j] ;
			}
		used[iMinPath] = 1 ;
		for (j = 1 ; j <= n ; ++j)
			if (!used[j] && dp[iMinPath] + map[iMinPath][j] < dp[j])
			{
				dp[j] = dp[iMinPath] + map[iMinPath][j] ;
			}
	}
}

int main ()
{
	int i, j ;
	while (scanf ("%d", &n), n)
	{
		scanf ("%d%d", &s, &e) ;
		memset (map, infinity, sizeof (map)) ;
		for (i = 1 ; i <= n ; ++i)
		{
			scanf ("%d", &j) ;
			// 注意不能越界
			if (SafeAccess (i - j))
				map[i][i-j] = 1 ;
			if (SafeAccess (i + j))
				map[i][i+j] = 1 ;
		}
		dijkstra () ;
		if (infinity != dp[e])
			printf ("%d\n", dp[e]) ;
		else // 一开始没怎么看题,把这句给忘了= =!
			puts ("-1") ;
	}
	return 0 ;
}


你可能感兴趣的:(杭电 hdu 1548 A strange lift)