洛谷 P1546 最短网络 Agri-Net Label:Water最小生成树
题目背景
农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场。当然,他需要你的帮助。
题目描述
约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场。为了用最小的消费,他想铺设最短的光纤去连接所有的农场。
你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并所用光纤最短的方案。每两个农场间的距离不会超过100000
输入输出格式
输入格式:
第一行: 农场的个数,N(3<=N<=100)。
第二行..结尾: 后来的行包含了一个N*N的矩阵,表示每个农场之间的距离。理论上,他们是N行,每行由N个用空格分隔的数组成,实际上,他们限制在80个字符,因此,某些行会紧接着另一些行。当然,对角线将会是0,因为不会有线路从第i个农场到它本身。
输出格式:
只有一个输出,其中包含连接到每个农场的光纤的最小长度。
输入输出样例
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
28
说明
题目翻译来自NOCOW。
USACO Training Section 3.1
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#define MAXN 20000
using namespace std; struct cc{
int from,to,cost;
}e[]; bool cmp(cc a,cc b){
return a.cost<b.cost;
} int fa[],N,map[][],p,ans,cnt; int Find(int x){
if(fa[x]==x)return x;
else return fa[x]=Find(fa[x]);
}
void unite(int a,int b){
int ta=Find(a),tb=Find(b);
if(ta==tb) return;
fa[ta]=tb;
} int main(){
// freopen("01.in","r",stdin);
scanf("%d",&N);
for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
scanf("%d",&map[i][j]);
}
}
for(int i=;i<=N;i++){
for(int j=i+;j<=N;j++){
e[++p].cost=map[i][j];
e[p].from=i;
e[p].to=j;
}
}
for(int i=;i<=N;i++) fa[i]=i;
sort(e+,e+p+,cmp);
for(int i=;i<=p;i++){
cc x=e[i];
if(Find(x.from)==Find(x.to)) continue;
unite(x.from,x.to);
if(cnt>=N-) break;
++cnt;
ans+=x.cost;
}
printf("%d\n",ans);
return ;
}
洛谷 P1546 最短网络 Agri-Net Label:Water最小生成树的更多相关文章
- 洛谷 P1546 最短网络 Agri-Net
题目链接 https://www.luogu.org/problemnew/show/P1546 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当 ...
- 洛谷P1546 最短网络 Agri-Net(最小生成树,Kruskal)
洛谷P1546 最短网络 Agri-Net 最小生成树模板题. 直接使用 Kruskal 求解. 复杂度为 \(O(E\log E)\) . #include<stdio.h> #incl ...
- 洛谷P1546 最短网络 Agri-Net
P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...
- 洛谷——P1546 最短网络 Agri-Net
P1546 最短网络 Agri-Net 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一 ...
- 洛谷 P1546 最短网络 Agri-Net x
题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...
- 洛谷 P1546 最短网络 Agri-Net(最小生成树)
嗯... 题目链接:https://www.luogu.org/problemnew/show/P1546 首先不难看出这道题的思想是用了最小生成树,但是这道题有难点: 1.读题读不明白 2.不会读入 ...
- 洛谷 P1546 最短网络 Agri-Net(最小生成树)
题目链接 https://www.luogu.org/problemnew/show/P1546 说过了不复制内容了 显然是个最小生成树. 解题思路 prim算法 Kruskal算法 prim算法很直 ...
- 洛谷P1546 最短网络 Agri-Net(Prim堆优化)
#include<bits/stdc++.h> using namespace std; ; const int INF=0x3f3f3f3f; inline void read(int ...
- 洛谷1546 最短网络Agri-Net【最小生成树】【prim】
[内含最小生成树Prim模板] 题目:https://www.luogu.org/problemnew/show/P1546 题意:给定一个邻接矩阵.求最小生成树. 思路:点少边多用Prim. Pri ...
随机推荐
- (2)Underscore.js常用方法
目录 1.集合相关方法 1.1.数组的处理 map(循环,有返回值),将返回的值依次存入一个新的数组 each(循环,无返回值 ...
- Memcached驱动(C#)
using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.IO; usin ...
- Pyqt QSS简单的Ui美化
什么是QSS QSS 是Qt StyleSheet 的简称,意思就是qt的样式表格,StyleSheet 可以像CSS一样的写样式.使页面美化跟代码层分开,利于维护. QSS的语法 同css一样,他也 ...
- 【openGL】指定着色模型
#include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...
- UVA11542 Square(高斯消元 异或方程组)
建立方程组消元,结果为2 ^(自由变元的个数) - 1 采用高斯消元求矩阵的秩 方法一: #include<cstdio> #include<iostream> #includ ...
- Reporting Services 的伸缩性和性能表现规划(转载)
简介 Microsoft? SQL Server? Reporting Services 是一个将集中管理的报告服务器具有的伸缩性和易管理性与基于 Web 和桌面的报告交付手段集于一身的报告平台.Re ...
- ubuntu kylin中如何截图
windows操作系统中,我通常使用的截图工具是QQ的“ctrl+alt+a”快捷键.但是在ubuntu中,linux qq常年不更新,我也就彻底放弃了使用了,反正ubuntu通常只是拿来开发.其实没 ...
- flex_宽度补全
宽度40px,另一个的补全宽度: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 【maven 报错】maven项目执行maven install时报错Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
在使用maven新建的web项目中,执行 执行如上的这两个操作,报错: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-co ...
- summary of k Sum problem and solutions in leetcode
I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...