H - Agri-Net
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
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 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.
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
题意:
给出N(3到100)个村庄数,输入N*N矩阵,a[ i ][ j ]代表 i 村庄到 j 村庄的距离,求能到达所有村庄的最短路径。
题意:
用kruskal算法求最小生成树。
AC:
#include<cstdio> #include<string.h> #include<algorithm> using namespace std; typedef struct { int from,to,len; }r; r road[5000]; //主要路径数组的大小 int root[110]; int cmp(r a,r b) { return a.len<b.len; } int find(int a) { int x=a,t; while(root[x]!=x) x=root[x]; while(x!=a) { t=root[a]; root[a]=x; a=t; } return x; } int main() { int n,i,j,sum=0,length; int pos[110][110]; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) root[i]=i; memset(pos,0,sizeof(pos)); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&pos[i][j]); sum=0; for(i=1;i<n;i++) for(j=i+1;j<=n;j++) { sum++; road[sum].from=i; road[sum].to=j; road[sum].len=pos[i][j]; } sort(road+1,road+sum+1,cmp); length=0; for(i=1;i<=sum;i++) { int fa,fb; fa=find(road[i].from); fb=find(road[i].to); if(fa==fb) continue; else { root[fa]=fb; length+=road[i].len; } } printf("%d\n",length); } return 0; }
总结:
road数组是用来装路径的起点终点和长度的,所以路径条数应该不止顶点数那么多而已。开4000一下就会RE,开5000的时候就AC了,开少于1000的时候就TLE,所以应该要尽量开大点。
相关推荐
【标题】"POJ1258-Agri-Net【Prim】" 是一个编程竞赛题目,来源于北京大学的在线评测系统POJ(Problem Online Judge)。这个题目主要涉及到图论中的一个重要算法——普里姆(Prim)算法。 【描述】"北大POJ1258-...
pku acm 1258 Agri-Net代码 最小生成树的prim算法,有详细的注释
【标题】中的“1258:Agri-Net(弗洛伊德)”指的是一道编程竞赛题目,该题目可能要求解决图论中的最短路径问题,使用了弗洛伊德算法作为解决方案。 【描述】中提到的“弗洛伊德思路”是指弗洛伊德算法的基本思想。...
"阿格里·斯蒂克"是一个农业物联网项目,它利用现代信息技术,特别是Python编程语言,来提升农业生产效率和智能化水平。在这个项目中,我们可能会遇到以下关键知识点: 1. **物联网(IoT)基础**:物联网是互联网、...
标题“POJ3414-Pots”是一个编程竞赛题目,源自北京大学的在线判题系统POJ(Problem Online Judge)。这个题目主要考察的是算法设计和实现能力,通常涉及计算机科学中的数据结构和算法知识。 解题报告是参赛者在...
Sen2Agri-package-{{sen2agri_version}}。zip srtm.zip Maja_3.2.2_TM.zip 该脚本可以与外部数据库一起使用。 的角色 nfs-mount:由于大量数据,特定于我们的项目使用NFS文件系统 maja:需要Sen2Agri服务 sen2...
"agribarter-landingpage:Agri-Barter跨平台应用程序的着陆页" 这个标题揭示了我们正在讨论的是一个名为Agri-Barter的跨平台应用程序的着陆页面。"Agri-Barter"很可能是一个专注于农业交易的平台,而“着陆页”是...
农业地图 一些用于可视化农业植物间距的代码 要求 matplotlib ... 与运行python agri_map.py默认如图所示运行,其结果。 也可以与命令行参数一起运行以修改输出(与-h标志一起运行以查看可用选项)。
在解决这类问题时,可以使用Prim算法或Kruskal算法来寻找最小生成树。 Prim算法从一个顶点开始,逐步增加边,每次选择当前未加入树中且与树中顶点连接的边中权重最小的一条,直至连接所有顶点。Kruskal算法则是按照...
my_vue 一个Vue.js项目构建设置# install dependenciesnpm install# serve with hot reload at localhost:8080npm run dev# build for production with minificationnpm run build# build for production and view ...
标题 "PyPI 官网下载 | agri_tech-0.1.7-py2.py3-none-any.whl" 指的是一个在Python Package Index(PyPI)上发布的软件包,名为 "agri_tech",版本为 "0.1.7"。这个软件包的格式是 wheel(whl),这是一种预编译的...
《Python库agri_tech-0.1.6-py2.py3-none-any.whl详解》 在Python的生态系统中,库是构建复杂应用程序的重要基石。它们提供了丰富的功能,使得开发者能够快速、高效地实现各种任务。本文将深入探讨一个名为`agri_...
【FY4圆盘转等经纬工具】是一款专为处理风云四号A星(FY4A)HDF格式卫星数据而设计的应用程序。风云四号是中国新一代静止气象卫星,其观测数据具有高分辨率和丰富的遥感信息,对于气象预报、环境监测等方面有着重要...
它的方法是与非政府组织Nutri-Fresh Farm&Agri-Hub合作,该非政府组织是由肯尼亚的自给自足农民和青年创建农业企业家的。 每个家庭签署了《家禽项目贷款协议》,其中规定他们有义务无偿还以成年家禽的形式支付雏鸡...
《PyPI官网下载 | agri_tech-0.1.6.tar.gz——探索Python农业技术库》 在Python的世界里,PyPI(Python Package Index)是开发者们分享和获取开源软件包的重要平台。"agri_tech-0.1.6.tar.gz"这个资源就是从PyPI...
它还强调了英语词根的重要性,并通过具体的单词示例,如agri-相关的词汇,来阐述字母在构词中的作用。 【标签】:英语历史、字母演变、词根、语言学 【正文】: 英语字母的演变历程是一部丰富多彩的语言发展史。...
Agri-antibiotics 2507 is a new kind of agri-antibiotics against Oomyces,its analysismethod was established with high performance liquid chromatography ( HPLC) . Agri-antibiotics 2507 could be ...
语言:English ...干针治疗,肌肉刺激(IMS)也称为肌肉内刺激的治疗。该方法已由加拿大麻醉医生开发。疼痛是一种方法,其中可以在不使用的情况下处理疼痛。可用于治疗急性和慢性疼痛。特别是,它用于治疗肌肉和骨骼疼痛...