`

【最短路+dijkstra】 2680 Choose the best route

阅读更多

 

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

	URL   : http://acm.hdu.edu.cn/showproblem.php?pid=2680
	Name  : 2680 Choose the best route

	Date  : Sunday, January 15, 2012
	Time Stage : half an hour

	Result: 

5259194	2012-01-15 10:24:58	Accepted	2680
250MS	4120K	1664 B
C++	pyy



Test Data :

Review :
Dijkstra 模板题~~
//----------------------------------------------------------------------------*/

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

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

#define INF		0x0f0f0f0f
#define MAXN	1002

bool	used[MAXN] ;

int		n, m, s, w ;
int		dist[MAXN], map[MAXN][MAXN] ;

int dijkstra ()
{
	int i, j ;
	int iMinPath, MinPath ;

	memset (used, 0, sizeof (used)) ;

	for (i = 1 ; i <= n ; ++i)
		dist[i] = map[0][i] ;

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

	if (dist[s] == INF)
		return -1 ;
	return dist[s] ;
}

int main ()
{
	int i, j ;
	int x, y, c ;

	while (~scanf ("%d%d%d", &n, &m, &s))
	{
		memset (map, INF,sizeof (map)) ;
		for (i = 1 ; i <= m ; ++i)
		{
			scanf ("%d%d%d", &x, &y, &c) ;
			map[x][y] = min (map[x][y], c) ;
		}
		scanf ("%d", &w) ;
		for (i = 1 ; i <= w ; ++i)
		{
			scanf ("%d", &x) ;
			map[0][x] = 0 ;	// 站点0 为 Kiki 的家,设家到x 的距离为 0
		}
		printf ("%d\n", dijkstra ()) ;
	}
	return 0 ;
}
 
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics