其他分享
首页 > 其他分享> > POJ - Highways(最小生成树)

POJ - Highways(最小生成树)

作者:互联网

题目链接:http://poj.org/problem?id=1751
Time Limit: 1000MS Memory Limit: 10000K Special Judge

Description

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 ith 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

Problem solving report:

Description: 给你一个n个点,点与点之间的边权为两点间距离,而且有m条边的距离为0,现在让你求这个图的最小生成树,且输出最小生成树上边权不为0的边。
Problem solving: 很明显是最小生成树问题,做最小生成树的时候直接把边存起来即可,最后输出一下,找生成树的时候输出也可以。

Accepted Code:​​​​​​​

/* 
 * @Author: lzyws739307453 
 * @Language: C++ 
 */
#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 755;
const int MAXM = 1005;
const int inf = 0x3f3f3f3f;
bool vis[MAXN];
int pre[MAXN];
double mp[MAXN][MAXN], dis[MAXN];
struct Point {
    double x, y;
}p[MAXN];
struct edge {
    int v;
    double w;
    edge() {}
    edge(int v, double w) : v(v), w(w) {}
    bool operator < (const edge &s) const {
        return s.w < w;
    }
};
void prim(int s, int n) {
    priority_queue <edge> Q;
    for (int i = 1; i <= n; i++) {
        vis[i] = 0;
        dis[i] = inf;
    }
    dis[s] = 0;
    Q.push(edge(s, dis[s]));
    while (!Q.empty()) {
        edge u = Q.top();
        Q.pop();
        if (vis[u.v])
            continue;
        vis[u.v] = 1;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && dis[j] > mp[u.v][j]) {
                pre[j] = u.v;
                dis[j] = mp[u.v][j];
                Q.push(edge(j, dis[j]));
            }
        }
    }
}
double dist(Point a, Point b) {
    double t = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    return t;
}
int main() {
    int n, m;
    while (~scanf("%d", &n)) {
        memset(pre, 0, sizeof(pre));
        for (int i = 1; i <= n; i++) {
            scanf("%lf%lf", &p[i].x, &p[i].y);
            for (int j = 1; j < i; j++)
                mp[i][j] = mp[j][i] = dist(p[i], p[j]);
        }
        scanf("%d", &m);
        for (int i = 0; i < m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            mp[u][v] = mp[v][u] = 0;
        }
        prim(1, n);
        for (int i = 1; i <= n; i++)
            if (mp[pre[i]][i])
                printf("%d %d\n", pre[i], i);
    }
    return 0;
}

标签:highways,int,最小,Highways,POJ,MAXN,towns,include,highway
来源: https://blog.csdn.net/lzyws739307453/article/details/99540964