POJ1751 Highways

本文解析了 POJ 1751 题目,通过 Prim 算法求解最小生成树问题。介绍了如何初始化图结构并利用 Prim 算法输出连接所有城市的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.原题链接:http://poj.org/problem?id=1751

二.题目大意:首先给出N个城市的坐标,再给出哪些城市已经相互连通,求花费最少的路,将所有城市连通,输出需要连通的城市。

三.思路:刚开始想用Kruskal做比较方便,因为它分开后森林,而且每课树的节点不唯一。但是还是练习一下Prim输出节点,其实只要把已经连通的城市的权置为0,然后在输出的时候先判断这条路的权值是不是为0,不是才输出,就可以了。

四.代码:

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

using namespace std;

const int MAX_SIZE = 755,
          INF = 0x3f3f3f3f,
          MOD = 1000000007;

int nodeNum, builtNum, nearVex[MAX_SIZE];
double x[MAX_SIZE], y[MAX_SIZE];
double graph[MAX_SIZE][MAX_SIZE], lowCost[MAX_SIZE];

void init()
{
    int i, j, cityA, cityB;

    cin>>nodeNum;

    for(i = 1; i <= nodeNum; i++)
        cin>>x[i]>>y[i];

    for(i = 1; i <= nodeNum; i++){
        for(j = i + 1; j <= nodeNum; j++)
            graph[j][i] = graph[i][j] =
            sqrt(pow(x[i] - x[j], 2.0) + pow(y[i] - y[j], 2.0) );
    }
    cin>>builtNum;

    for(i = 1; i <= builtNum; i++){
        cin>>cityA>>cityB;
        graph[cityA][cityB] = graph[cityB][cityA] = 0;
    }
}

void Prim(int start)
{
    int i, j, v;
    double minEdge;
    for(i = 1; i <= nodeNum; i++){
        lowCost[i] = graph[start][i];
        nearVex[i] = start;
    }

    nearVex[start] = -1;

    for(i = 1; i < nodeNum; i++){
        minEdge = INF;
        v = -1;
        for(j  = 1; j <= nodeNum; j++)
            if(nearVex[j] != -1 && lowCost[j] < minEdge){
                minEdge = lowCost[j];
                v = j;
            }

        if(graph[v][nearVex[v]] != 0.0)
            cout<<nearVex[v]<<" "<<v<<endl;

        nearVex[v] = -1;

        for(j = 1; j <= nodeNum; j++)
            if(nearVex[j] != -1 && lowCost[j] > graph[v][j]){
                lowCost[j] = graph[v][j];
                nearVex[j] = v;
            }
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);

    init();

    Prim(1);
}


POJ 1751是一道经典的图论问题,题目描述如下: 给定一个无向图,图中有N个节点和M条边。每条边都有一个权值。现在需要在这个图中增加最少数量的边,使得图中的任意两个节点之间都有一条路径,并且增加边的总权值最小。 这是一个典型的最小生成树(Minimum Spanning Tree, MST)问题。解决这个问题的常用算法是Kruskal算法和Prim算法。这里我们使用Kruskal算法来解决问题。 Kruskal算法的基本步骤如下: 1. 将所有边按权值从小到大排序。 2. 初始化一个并查集(Union-Find Set),将每个节点作为一个独立的集合。 3. 依次选择权值最小的边,如果这条边连接的两个节点不在同一个集合中,则将这条边加入最小生成树,并将这两个节点所在的集合合并。 4. 重复步骤3,直到所有节点都在同一个集合中,或者已经选择了N-1条边(N是节点的数量)。 下面是一个Java实现的示例代码: ```java import java.util.Arrays; import java.util.Scanner; public class Main { static int[] parent; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int M = scanner.nextInt(); Edge[] edges = new Edge[M]; parent = new int[N + 1]; for (int i = 0; i < M; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); int cost = scanner.nextInt(); edges[i] = new Edge(x, y, cost); } Arrays.sort(edges); for (int i = 1; i <= N; i++) { parent[i] = i; } int totalCost = 0; int edgeCount = 0; for (Edge edge : edges) { if (find(edge.x) != find(edge.y)) { union(edge.x, edge.y); totalCost += edge.cost; edgeCount++; if (edgeCount == N - 1) { break; } } } System.out.println(totalCost); scanner.close(); } static int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } static void union(int x, int y) { parent[find(x)] = find(y); } static class Edge implements Comparable<Edge> { int x, y, cost; Edge(int x, int y, int cost) { this.x = x; this.y = y; this.cost = cost; } @Override public int compareTo(Edge other) { return this.cost - other.cost; } } } ``` 这个代码首先读取节点和边的数量,然后读取每条边的信息并存储在Edge数组中。接着对边按权值进行排序,并初始化并查集。然后依次选择权值最小的边,如果这条边连接的两个节点不在同一个集合中,则将这条边加入最小生成树,并将这两个节点所在的集合合并。最终输出最小生成树的总权值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值