Agri-Net POJ - 1258 (最小生成树)

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int G[105][105];
int n;
struct node{
    int from,to;
    int len;
}farm[10005];
bool cmp(node a,node b){
    return a.len < b.len;
}
int pre[105];
int Find(int x){
    if(x == pre[x])
        return x;
    else return pre[x] = Find(pre[x]);
}
int Union(int x,int y){
    int fx = Find(x),fy = Find(y);
    if(fx == fy)
        return 0;
    else{
        pre[fy] = fx;
        return 1;
    }
}
int main(){
    while(~scanf("%d",&n)){
        int i,j;
        for(i = 0; i < 105; i++)
            pre[i] = i;
        int cnt = 0;
        for(i = 0; i < n; i++){
            for(j = 0; j < n; j++){
                scanf("%d",&G[i][j]);
                if(G[i][j] != 0){
                    farm[cnt].from = i;
                    farm[cnt].to = j;
                    farm[cnt++].len = G[i][j];
                }
            }
        }
        sort(farm,farm+cnt,cmp);
        int num = 0;
        int sum = 0;
        for(i = 0; i < cnt; i++){
            if(Union(farm[i].from,farm[i].to)){
                sum += farm[i].len;
                num++;
            }
            if(num == n-1)
                break;
        }
        cout << sum << endl;
    }
    return 0;
}

POJ1258 Agri-Net中英对照翻译与参考解答 POJ1258 Agri-Net 传送门: POJ1258 关键词: 图;Prim Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs... 阅读详情

相关推荐

poj1258 Agri-Net 最小生成树 prim kruskal 模板

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56702   Accepted: 23513 Description Farmer John has been elected mayor of his town! One of his campa

. 4856

1258:Agri-Net(弗洛伊德)

1258:Agri-Net 弗洛伊德思路: 先找到与1相连最短的那条路,然后再更新与1和刚才相连的点每个与其他点最短的路存储,然后再找到下一个点,再找到这三个点与其他点最短路存储,以此类推。 Prim代码: 除了最后一个ac代码是自己的其他的都不是,wa的全是自己进步的过程,加油! #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ll

数据结构 图的最小生成树 C++描述 使用prim算法、kruskal算法

分别利用prim算法和kruskal算法实现求图的最小生成树 C++描述

POJ_1258_Farmer John has been【基础最小生成树

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring in

持之以恒_fighting 3276

POJ 1258 - Agri-Net》的题解

POJ 1258 - Max Sum Plus Plus》的题解 题目的描述 Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of...

weixin_46163391的博客 712

Agri-Net最小生成树

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high spee...

WhileTrue 368

最小生成树

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high spee

baoluqi的博客 448

prim算法 poj1258 Agri-Net 最小生成树基础题

Agri-NetTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 64460 Accepted: 26642DescriptionFarmer John has been elected mayor of his town! One of his campaign promises was to bring internet con...

bestsort 482

最小生成树POJ1258Agri-Net

POJ1258Agri-Net 题意&思路: 给n个农场之间的距离,要铺设光缆使之两两连通,问最小铺设光缆的距离。 模板题。 代码: #include<iostream> #include<stdio.h> #include<stdlib.h> #include<algorithm> #include<string.h> #inc...

Z7784562的博客 394

POJ 1258 Agri-Net (prim 最小生成树)

题目链接: POJ 1258 Agri-Net (prim 最小生成树) Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He n

Dextrad_ihacker的博客 642

POJ1258 Agri-Net最小生成树

题目链接:http://poj.org/problem?id=1573 解题思路: 最小生成树!!! AC代码: #include #include #include #include using namespace std; const int maxn=100010; int pa[maxn]; int n,m,sum; struct Edge { int

piaocoder 566

poj 1258 Agri-Net 基本&经典最小生成树

传送门:poj 1258 Agri-Net题目大意n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输入的是邻接矩阵,输出安装光纤的总距离解题思路裸prim, 1. 随便找一个元素进行寻找 2. 找出和这个元素相邻的所有权值,如果不能到达表示无限大 3. 找到和这个元素相邻的最小权值,记录下来 4. 最小权值

woshinannan的专栏 359

POJ 1258 Agri-Net 最小生成树

POJ 1258 Agri-Net 最小生成树  题意:有n个点,给你一个方阵代表他们两两之间的距离,求最小生成树。  思路:大水题,直接prim解决。AC代码:// // main.cpp // L // // Created by LucienShui on 2017/5/11. // Copyright © 2017年 LucienShui. All rights reserved.

LucienShui 578

poj1258 - Agri-Net (经典邻接矩阵 求最小生成树) (Prim & Kruskal)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40028   Accepted: 16291 Description Farmer John has been elected mayor of his town! One of his campaig

ITCTO-疯男的CSDN博客 872

POJ 1258 Agri-Net(最小生成树)

POJ 1258 Agri-Net(最小生成树) http://poj.org/problem?id=1258 题意:        给你一个N顶点的无向图的距离邻接矩阵,问你使得整个图连通的最小边长和是多少? 分析:        最小生成树入门题,直接kruskal模板. AC代码: #include #include #include using namespace std

code 1026

最小生成树(Kruskal)POJ 1258 Agri-Net

最小生成树(Kruskal)POJ 1258 Agri-Net 题目链接: Agri-Net 题目基础: 最小生成树-Kruskal(克鲁斯卡尔)算法 思路: 题目大意: 有N个村庄,输入每个村庄到编号为1~N的距离,每个村落都需要网络覆盖,问网线的最短长度 题解: 最小生成树的裸题,按模板打 说个优化吧,样例给的是一个矩阵,但很容易想到,1到3的距离和...

[SZU_Crayon] 502

POJ 1258 Agri-Net最小生成树入门题目】

原题链接:http://poj.org/problem?id=1258 周四上了节数据结构终于知道何为最小生成树,晚上回寝,遂得此题! Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29613   Accepted: 11750 Des

free斩 5507

POJ-1258 Agri-Net(最小生成树+Prim算法)

POJ-1258 Agri-Net(最小生成树+Prim算法) Agri-NetTime Limit: 1000MS Memory Limit: 10000K   DescriptionFarmer John has been elected mayor of his town! One of his campaign promises was to bring internet connecti...

碳酸钙的01妖精的博客 250

POJ 1258 Agri-Net [最小生成树] 《挑战程序设计竞赛》2.5

POJ 1258 Agri-Net最小生成树

yoer77的博客 350
上一篇: Six Degrees of Cowvin Bacon POJ - 2139 (floyd求最短路)
下一篇: Candies POJ - 3159 (差分约束+最短路)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值