最小生成树 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 / ...
随机推荐
- Javascript对象基础讲解
1.Object对象详解 javascript 里最基本的数据类型是对象. avaScript里的对象其实是一个无序的属性集合,属性又是一个个的名-值对. 除了字符串,数字,true,false,nu ...
- EditText(4)常用属性详解
常用的属性: 显示密码 通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码. editText.setTransformationMethod( ...
- git add . 的时候报错fatal: Unable to create : …File exists.
报错内容: $ git add . fatal: Unable to create 'E:/project/qbm_cs/.git/index.lock': File exists. Another ...
- (2)麻省理工:计算机科学和 Python 编程导论
语义描述了我们如何从那些表达式中推导出相关的含义,从而解决我们想解决的问题. 语法描述了如何将合法表达式组合在一起. 我们要选择什么样的编程语言? 1. 不管我们选什么,都有如下过程: 输入信 ...
- 总结esp8266刷Python的完整的步骤(终极总结)
2018-04-0319:12:02 从玩microPython 到现在,一路荆棘一路坎坷. 不知道只有我遇到这样的问题还是microPython太不稳定,还是我买的板子太糙.总之遇到了太多问题了. ...
- vue具体页面跳转传参方式
1.写数据,可以使用“.”,”[]”,以及setItems(key,value);3种方式. 例如: localStorage.name = proe;//设置name为" proe &qu ...
- GCC编译连接c++代码的四个阶段(Four stages of GCC compilation of C++ code)
There are four stages for GCC to compile c/c++ applications: Preprocessing, Compilation proper, Asse ...
- swift 扩展 要素总结
类: 协议: 泛型及元素类型:扩展约束:
- eclipse 新建 maven 项目 + 消除错误
安装eclips以及maven自行去了解,这里不讲解 一.新建一个 maven 项目. 二.下一步选择项目架构 三.填写相关信息 group id: 一般都是 com点 项目名 aftriact.id ...
- java 23种设计模式 链接集锦
创建型抽象工厂模式 http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html工厂方法 http://www.cnblogs ...