最小生成树 I - Agri-Net
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
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28 Kruskal 算法,借助并查集
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 101
#define INF 0x3f3f3f3f
int pre[MAXN];
struct Edge
{
int u,v,w;
}edge[MAXN*MAXN/];
int tol;
void addedge(int u,int v,int w)
{
edge[tol].u = u;
edge[tol].v = v;
edge[tol++].w = w;
}
bool cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int find(int x)
{
if(pre[x]==-)
return x;
else
return pre[x] = find(pre[x]);
}
int Kruskal(int n)
{
memset(pre,-,sizeof(pre));
sort(edge,edge+tol,cmp);
int cnt = ;
int ans = ;
for(int i=;i<tol;i++)
{
int u = edge[i].u;
int v = edge[i].v;
int w = edge[i].w;
int t1 = find(u),t2 = find(v);
if(t1!=t2)
{
ans+=w;
pre[t1] = t2;
cnt++;
}
if(cnt==n-) break;
}
if(cnt<n-) return -;
else return ans;
}
int main()
{
int n,d;
while(cin>>n)
{
//if(n==0) break;
tol = ;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>d;
if(j>i)
addedge(i,j,d);
}
}
int ans = Kruskal(n);
cout<<ans<<endl;
}
return ;
}
最小生成树 I - Agri-Net的更多相关文章
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- 最小生成树(Kruskal算法-边集数组)
以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...
- 最小生成树计数 bzoj 1016
最小生成树计数 (1s 128M) award [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- 【BZOJ 1016】【JSOI 2008】最小生成树计数
http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...
- 最小生成树---Prim算法和Kruskal算法
Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...
- Delaunay剖分与平面欧几里得距离最小生成树
这个东西代码我是对着Trinkle的写的,所以就不放代码了.. Delaunay剖分的定义: 一个三角剖分是Delaunay的当且仅当其中的每个三角形的外接圆内部(不包括边界)都没有点. 它的存在性是 ...
- 最小生成树(prim&kruskal)
最近都是图,为了防止几次记不住,先把自己理解的写下来,有问题继续改.先把算法过程记下来: prime算法: 原始的加权连通图——————D被选作起点,选与之相连的权值 ...
- 最小生成树 prime poj1258
题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...
- 最小生成树 prime + 队列优化
存图方式 最小生成树prime+队列优化 优化后时间复杂度是O(m*lgm) m为边数 优化后简直神速,应该说对于绝大多数的题目来说都够用了 具体有多快呢 请参照这篇博客:堆排序 Heapsort / ...
随机推荐
- linux vi 块操作、多窗口
vim 块选择v:字符选择或者行选择[ctrl]+v: 块选择y:将反白的地方复制d:将反白的地方删除 多窗口:sp {filename} 打开一个新的窗口[ctrl]+w+j或者[ctrl]+w+向 ...
- HDU 1879(最小生成树)
#include "iostream" #include "algorithm" #include "cstdio" using names ...
- C#中接受一个非字符串的输入
接受来自用户的值 System 命名空间中的 Console 类提供了一个函数 ReadLine(),用于接收来自用户的输入,并把它存储到一个变量中. 例如: int num; num = Conve ...
- URI URL URN的区别
一:什么是URI,URL,URN ? URI:Uniform Resource Identifier,统一资源标识符,是一个用于表示互联网上资源名称的字符串 格式:http://www.xxx.com ...
- Selenium基于Python web自动化基础一 -- 基础汇总及简单操作
Selenium是UI层WEB端的自动化测试框架,也是目前市面上比较流行的自动化测试框架. ui层自动化测试本质是什么?模拟用户的真实操作行为. 基础汇总: 导入所需要的模块 from seleniu ...
- 求助:可以使用任何编程工具做成一个控件或组件,使得在VB中能调用并得到摄像头的参数及图片。
请看下网址上的这个问题,看是否有解决的方式http://www.educity.cn/wenda/338634.html
- Farseer.net轻量级开源框架 中级篇:动态数据库访问
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 自定义配置文件 下一篇:Farseer.net轻量级开源框架 中级篇: 数据库切换 ...
- Learning Face Age Progression: A Pyramid Architecture of GANs
前言 作为IP模式识别的CNN初始模型是作为单纯判别式-模式识别存在的,并以此为基本模型扩展到各个方向.基本功能为图像判别模型,此后基于Loc+CNN的检测模型-分离式.end2end.以及MaskC ...
- 【Linux】 JDK安装及配置 (linux-tar.gz版)
安装环境:Linux(CentOS 7 64位 版) JDK安装:tar.gz为解压后就可以使用的版本,这里使用jdk-8u211-linux-x64.tar.gz版,安装到/usr/java/(us ...
- PHP 之QQ第三方登录
一.下载QQ SDK 下载地址:http://wiki.open.qq.com/wiki/mobile/SDK 二.配置SDK 三.具体代码 login.html <!DOCTYPE html& ...