POJ 2502 Subway

题目大意:

题目链接

注释代码:

无注释代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>

#define	INF		1E15
#define	MAXN	202

#define	V_W		10
#define	V_S		40

using namespace std;

struct	Point {

	int		x, y;

	friend istream &
	operator>>(istream &is, Point &p) {
	
		is >> p.x >> p.y;
		return is;
	}

	int
	POW(int a) { return a * a; }

	double
	operator^(Point &oth) {
	
		return sqrt( (double)POW( x - oth.x ) + POW( y - oth.y ) );
	}
};

struct	Node {

	int		u;
	double	d;

	Node(void) {}
	Node(int uu, double dd) : u(uu), d(dd) {}

	bool
	operator<(const Node &oth)
	const {
	
		return d > oth.d;
	}
};

double	g[MAXN][MAXN];
Point	p[MAXN];
double	d[MAXN];
bool	vis[MAXN];

priority_queue<Node>	heap;

double
dij(int n) {

	int		u, v;
	int		i;

	Node	node;

	for ( i = 1; i < n; i++ ) d[i] = INF;
	heap.push(Node(0, 0));

	while ( !heap.empty() ) {
	
		node = heap.top();
		heap.pop();

		if ( 1 == ( u = node.u ) ) return d[1];
		if ( vis[u] ) continue;

		vis[u] = true;
		for ( v = 1; v < n; v++ )
			if ( !vis[v] && d[u] + g[u][v] < d[v] ) {

				d[v] = d[u] + g[u][v];
				heap.push(Node(v, d[v]));
			}
	}

	return -1.0;
}

int
main() {

	int		n;
	int		x, y;
	int		i, j;

	cin >> p[0] >> p[1];
	n = 1;
	while ( cin >> p[++n] ) {

		while ( ( cin >> p[++n] ), p[n].x != -1 )	
			g[n][n - 1] = g[n - 1][n] = ( p[n] ^ p[n - 1] ) * 3 / ( V_S * 50 );
		n--;
	}

	for ( i = 0; i < n; i++ )
		for ( j = i + 1; j < n; j++ )
			if ( !g[i][j] )
				g[i][j] = g[j][i] = ( p[i] ^ p[j] ) * 3 / ( V_W * 50 );

	printf("%.0lf\n", dij(n));

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值