您的位置:首页 > 其它

zoj 2048 - Highways

2018-01-24 12:28 405 查看
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some
of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus
their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length.
Thus, the least expensive highway system will be the one that minimizes the total highways length. 

Input
The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built. 

The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of i th town (for i from
1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location. 

The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a
highway. Each pair of towns is connected by at most one highway. 

Output
Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway
connects, separated by a space. 

If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty. 

Sample Input
9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2


Sample Output
1 6
3 7
4 9
5 7
8 3

题意:给出n个点的坐标,然后给出m,代表m个(u,v),表示u点和v点连通,求最小生成树,然后输出再需要多少边连通,才能形成最小生成树,依次输出两个点(不要求顺序)

Kruskal,将提前给出的m条边直接存入并查集,排序之后,开始读边,如果边满足,就输出,知道形成最小生成树之后,停止输出,这是ZOJ的题,不过好像形式变了。

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int f[755];
struct node{
int x, y;
}s[755];
struct edge{
int x, y;
double cost;
void Edge(int x1, int y1, double dis) {
x = x1; y = y1; cost = dis;
}
}e[600000];
bool cmp(edge a, edge b) {
return a.cost < b.cost;
}
double Distance(node a, node b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int getf(int v) {
if(f[v] != v) {
f[v] = getf(f[v]);
}
return f[v];
}
void init(int n) {
for(int i = 1; i <= n; i++) {
f[i] = i;
}
}
bool cmp1(node a, node b) {
return a.x < b.x;
}
int main() {
int N, M;
int u, v;
scanf("%d", &N);
init(N);  //初始化
for(int i = 1; i <= N; i++) {
scanf("%d %d", &s[i].x, &s[i].y);
}
scanf("%d", &M);
for(int i = 0; i < M; i++) {
scanf("%d %d", &u, &v);//存入
int t1 = getf(u);
int t2 = getf(v);
if(t1 != t2)
f[t1] = t2;
}
int t = 0;
for(int i = 1; i < N; i++) {
for(int j = i+1; j <= N; j++) {
double Dis = Distance(s[i], s[j]);
e[t++].Edge(i, j, Dis);
e[t++].Edge(j, i, Dis);
}
}
sort(e, e+t, cmp);
for(int i = 0; i < t; i++) {
int t1 = getf(e[i].x);
int t2 = getf(e[i].y);
if(t1 != t2) {
f[t1] = t2;
printf("%d %d\n", e[i].x, e[i].y);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: