POJ 1751 Highways

题目大意:

        只有一个测例,有N个点编号1 ~ N(1 ≤ N ≤ 750),并给出N个点的坐标"X Y",其中0 ≤ X, Y ≤ 10,000,都是整型,接下来会给出M条已经存在的边(0 ≤ M ≤ 1,000),每条边都给出两个端点的编号,问题是还要添加几条边才能使图上所有点都连通并且是新添加的边的总长度最小,只要求输出添加的每条边,每条边包含两个点的编号(用空格隔开),每条边占一行,每条边中点的顺序以及边的输出顺序任意。

题目链接

注释代码:

/*                                    
 * Problem ID : POJ 1751 Highways 
 * Author     : Lirx.t.Una                                    
 * Language   : C++                        
 * Run Time   : 79 ms                                    
 * Run Memory : 2880 KB                                    
*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

//点的最大数量
#define	MAXN	750

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; }

	int
	operator^(Point &oth) {//计算两点距离的平方
	
		return POW( x - oth.x ) + POW( y - oth.y );
	}
};

struct	Arc {//边的信息,输出答案时用到

	int		u, v;//边的两个端点

	Arc(int uu, int vv) : u(uu), v(vv) {}

	friend ostream &
	operator<<(ostream &os, Arc &arc) {
	
		os << arc.u << ' ' << arc.v;
		return os;
	}
};

struct	Node {//Prim结点

	int		u;
	int		d;

	Node(void) {}

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

	bool
	operator<(const Node &oth)
	const {

		return d > oth.d;
	}
};

Point	p[MAXN + 1];
int		g[MAXN + 1][MAXN + 1];

int		pre[MAXN + 1];
int		d[MAXN + 1];
bool	vis[MAXN + 1];

queue<Arc>		ans;
priority_queue<Node>	heap;

void
prim(int n) {

	int		i;
	int		nv;

	int		u, v;

	Node	node;

	vis[1] = true;
	for ( i = 2; i <= n; i++ ) {
	
		d[i]   = g[1][i];
		pre[i] = 1;

		heap.push(Node( i, d[i] ));
	}

	nv = 1;
	while ( !heap.empty() ) {//输入会出现无法构成生成树的情况!!蛋疼
	
		while (true) {
		
			node = heap.top();
			heap.pop();

			if ( !vis[ u = node.u ] ) {
			
				vis[u] = true;
				nv++;

				if ( g[ pre[u] ][u] ) ans.push(Arc(pre[u], u));//不是已经造好的路就放入ans队列中
				break;
			}
		}

		if ( nv == n ) break;

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

				d[v]   = g[u][v];
				pre[v] = u;

				heap.push(Node( v, d[v] ));
			}
	}

	while ( !ans.empty() ) {//将需要新建的路输出
	
		cout << ans.front() << endl;
		ans.pop();
	}
}

int
main() {

	int		n, m;//点数,已经修好的路的数量
	int		u, v;//临时点
	int		i, j;

	scanf("%d", &n);
	for ( i = 1; i <= n; i++ ) cin >> p[i];
	for ( i = 1; i <= n; i++ )
		for ( j = i + 1; j <= n; j++ )
			g[i][j] = g[j][i] = p[i] ^ p[j];

	scanf("%d", &m);
	while ( m-- ) {
	
		scanf("%d%d", &u, &v);
		g[u][v] = g[v][u] = 0;//已经修好的路就不需要耗费
	}

	prim(n);

	return 0;
}
无注释代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

#define	MAXN	750

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; }

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

struct	Arc {

	int		u, v;

	Arc(int uu, int vv) : u(uu), v(vv) {}

	friend ostream &
	operator<<(ostream &os, Arc &arc) {
	
		os << arc.u << ' ' << arc.v;
		return os;
	}
};

struct	Node {

	int		u;
	int		d;

	Node(void) {}

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

	bool
	operator<(const Node &oth)
	const {

		return d > oth.d;
	}
};

Point	p[MAXN + 1];
int		g[MAXN + 1][MAXN + 1];

int		pre[MAXN + 1];
int		d[MAXN + 1];
bool	vis[MAXN + 1];

queue<Arc>		ans;
priority_queue<Node>	heap;

void
prim(int n) {

	int		i;
	int		nv;

	int		u, v;

	Node	node;

	vis[1] = true;
	for ( i = 2; i <= n; i++ ) {
	
		d[i]   = g[1][i];
		pre[i] = 1;

		heap.push(Node( i, d[i] ));
	}

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

			if ( !vis[ u = node.u ] ) {
			
				vis[u] = true;
				nv++;

				if ( g[ pre[u] ][u] ) ans.push(Arc(pre[u], u));
				break;
			}
		}

		if ( nv == n ) break;

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

				d[v]   = g[u][v];
				pre[v] = u;

				heap.push(Node( v, d[v] ));
			}
	}

	while ( !ans.empty() ) {
	
		cout << ans.front() << endl;
		ans.pop();
	}
}

int
main() {

	int		n, m;
	int		u, v;
	int		i, j;

	scanf("%d", &n);
	for ( i = 1; i <= n; i++ ) cin >> p[i];
	for ( i = 1; i <= n; i++ )
		for ( j = i + 1; j <= n; j++ )
			g[i][j] = g[j][i] = p[i] ^ p[j];

	scanf("%d", &m);
	while ( m-- ) {
	
		scanf("%d%d", &u, &v);
		g[u][v] = g[v][u] = 0;
	}

	prim(n);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值