【最短路+dijkstra+spfa】杭电 hdu 2722 Here We Go(relians) Again

 

Dijkstra 解法

 

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

	URL   : http://acm.hdu.edu.cn/showproblem.php?pid=2722
	Name  : 2722 Here We Go(relians) Again

	Date  : Friday, January 20, 2012
	Time Stage : 4 hours

	Result: 
5281667	2012-01-20 23:30:18	Accepted	2722
0MS	1272K	2232 B
C++	pyy


Test Data :

Review :
这题其实没什么难度,就是题目特别恶心,又臭又长,再加上制图也很恶心,
第一次做这么恶心的题,于是乎拖了几个小时。
把输入分为横向路 和 纵向路 来处理会比较方便一点,这个想了好久啊……好笨好笨
//----------------------------------------------------------------------------*/

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

#define INF		0x3f3f3f3f
#define MAXN	522

#define min(x, y)	((x) < (y) ? (x) : (y))
#define max(x, y)	((x) > (y) ? (x) : (y))
#define MEM(a, v)	memset (a, v, sizeof (a))

bool	used[MAXN] ;

int		v, h ;
int		dist[MAXN], map[MAXN][MAXN] ;

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

	MEM (used, 0) ;
	for (i = 1 ; i <= end ; ++i)
		dist[i] = map[start][i] ;
	dist[start] = 0 ;

	for (i = 1 ; i <= end ; ++i)
	{
		iMinPath = 0 ;
		MinPath = INF ;

		for (j = 1 ; j <= end ; ++j)
		{
			if (!used[j] && dist[j] < MinPath)
			{
				iMinPath = j ;
				MinPath = dist[j] ;
			}
		}
		used[iMinPath] = 1 ;
		for (j = 1 ; j <= end ; ++j)
		{
			if (!used[j])
				dist[j] = min(dist[iMinPath]+map[iMinPath][j], dist[j]) ;
		}
	}
	return dist[end] ;
}

int main ()
{
	int i, j ;
	int x, y, d, d1, d2, ver, hor ;
	int ret ;

	char c ;
	
	while (scanf ("%d%d", &v, &h), v | h)
	{
		MEM (map, INF) ;
		ver = 1, hor = 1 ;
		for (i = 1 ; i <= 2 * v + 1 ; ++i)
		{
			for (j = 1 ; j <= h + !(i&1) ; ++j)
			{
				scanf ("%d %c", &d, &c) ;
				// 如果d为0,表示此路不通
				if (d == 0)
					c = 0 ;

				if (i & 1)	// 横向的路
				{
					x = j + (h+1)*(hor-1) ;
					y = x + 1 ;
					d1 = (c == '*' || c == '>') ? 2520/d : INF ;
					d2 = (c == '*' || c == '<') ? 2520/d : INF ;
				}
				else		// 纵向的路
				{
					x = j + (h+1)*(ver-1) ;
					y = j + (h+1)*(ver) ;
					d1 = (c == '*' || c == 'v') ? 2520/d : INF ;
					d2 = (c == '*' || c == '^') ? 2520/d : INF ;
				}
				map[x][y] = d1 ;
				map[y][x] = d2 ;
			}
			if (i & 1)		++hor ;		// 横向逢单加 1
			if (!(i & 1))	++ver ;		// 纵向逢双加 1

			getchar () ;
		}
		ret = dijkstra (1, (v+1)*(h+1)) ;

		if (INF == ret)
			printf ("Holiday\n") ;
		else
			printf ("%d blips\n", ret) ;
	}
	return 0 ;
}

 

Spfa 解法

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

	URL   : http://acm.hdu.edu.cn/showproblem.php?pid=2722
	Name  : 2722 Here We Go(relians) Again

	Date  : Friday, January 20, 2012
	Time Stage : 

	Result: 
5281754	2012-01-20 23:50:26	Accepted	2722
15MS	1284K	2556 B
C++	pyy


Test Data :

Review :
Spfa 算法居然比 Dijkstra 慢,可能 Spfa 在用链接表的时候才比 Dijkstra 有优势
//----------------------------------------------------------------------------*/

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

using namespace std ;

#define INF		0x3f3f3f3f
#define MAXN	522

#define min(x, y)	((x) < (y) ? (x) : (y))
#define max(x, y)	((x) > (y) ? (x) : (y))
#define MEM(a, v)	memset (a, v, sizeof (a))

bool	used[MAXN] ;

int		v, h ;
int		dist[MAXN], map[MAXN][MAXN] ;

int spfa (const int start, const int end)
{
	int i, t ;
	
	queue<int>		q ;

	MEM (used, 0) ;
	MEM (dist, INF) ;

	q.push (start) ;
	used[start] = 1 ;

	// 起始点必须为 0 ,否则接下来 dist[t] + map[t][i] 的值永远都大于 INF
	dist[start] = 0 ;

	while (!q.empty())
	{
		t = q.front () ;
		q.pop () ;

		for (i = 1 ; i <= end ; ++i)
		{
			// 注意 dist[t] + map[t][i] 可能会出现 INF + INF 为负值的问题
			if (dist[t] + map[t][i] < dist[i])
			{
				dist[i] = dist[t] + map[t][i] ;
				// 先更新最短路,不管该点是否已经入队
				// 若未入队,则加入,为最短路作延伸
				if (!used[i])
				{
					used[i] = 1 ;
					q.push (i) ;
				}
			}
		}
		// 从 t 点出发不一定能找到最短路,从其他点到 t 点则有可能
		// 所以 t 点应该出队,使其有可能再次入队
		used[t] = 0 ;
	}

	return dist[end] ;
}

int main ()
{
	int i, j ;
	int x, y, d, d1, d2, ver, hor ;
	int ret ;

	char c ;
	
	while (scanf ("%d%d", &v, &h), v | h)
	{
		MEM (map, INF) ;
		ver = 1, hor = 1 ;
		for (i = 1 ; i <= 2 * v + 1 ; ++i)
		{
			for (j = 1 ; j <= h + !(i&1) ; ++j)
			{
				scanf ("%d %c", &d, &c) ;
				// 如果d为0,表示此路不通
				if (d == 0)
					c = 0 ;

				if (i & 1)	// 横向的路
				{
					x = j + (h+1)*(hor-1) ;
					y = x + 1 ;
					d1 = (c == '*' || c == '>') ? 2520/d : INF ;
					d2 = (c == '*' || c == '<') ? 2520/d : INF ;
				}
				else		// 纵向的路
				{
					x = j + (h+1)*(ver-1) ;
					y = j + (h+1)*(ver) ;
					d1 = (c == '*' || c == 'v') ? 2520/d : INF ;
					d2 = (c == '*' || c == '^') ? 2520/d : INF ;
				}
				map[x][y] = d1 ;
				map[y][x] = d2 ;
			}
			if (i & 1)		++hor ;		// 横向逢单加 1
			if (!(i & 1))	++ver ;		// 纵向逢双加 1

			getchar () ;
		}
		ret = spfa (1, (v+1)*(h+1)) ;

		if (INF == ret)
			printf ("Holiday\n") ;
		else
			printf ("%d blips\n", ret) ;
	}
	return 0 ;
}

 

你可能感兴趣的:(图论,最短路,dijkstra,SPFA)