最小生成树 --- 求最小权值、MST
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 37109 | Accepted: 14982 |
Description
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
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
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
【题目来源】
【题目大意】
给定一个强连通图,让你求最小生成树的权值之和。
【题目分析】
数据很水,用Kruskal或者prim都能水过。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#define MAX 600*300
using namespace std;
struct Node
{
int a,b,c;
};
Node node[MAX];
int parent[MAX];
int num[][];
int sum;
int temp; int Find(int x)
{
return x==parent[x]?x:parent[x]=Find(parent[x]);
} void Kruskal()
{
int x,y;
int i,j;
for(i=;i<temp;i++)
{
x=node[i].a;
y=node[i].b;
x=Find(x);
y=Find(y);
if(x!=y)
{
parent[x]=y;
sum+=node[i].c;
}
}
} bool cmp(Node a,Node b)
{
return a.c<b.c;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
sum=;
int i,j;
for(i=;i<MAX;i++)
parent[i]=i;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
scanf("%d",&num[i][j]);
}
}
temp=;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
node[++temp].a=i;
node[temp].b=j;
node[temp].c=num[i][j];
}
}
sort(node,node+temp,cmp);
Kruskal();
printf("%d\n",sum);
}
return ;
}
最小生成树 --- 求最小权值、MST的更多相关文章
- POJ-2195 Going Home---KM算法求最小权值匹配(存负边)
题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...
- poj3565 Ants km算法求最小权完美匹配,浮点权值
/** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...
- ZOJ-2342 Roads 二分图最小权值覆盖
题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...
- poj 3565 uva 1411 Ants KM算法求最小权
由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...
- POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)
题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...
- POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你 ...
- POJ 2195 Going Home 【二分图最小权值匹配】
传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS Memory Limit ...
随机推荐
- rabbitMq 学习笔记(一)
消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题 实现高性能,高可用,可伸缩和最终一致性架构. RabbitMQ 是采用 Erlang 语言实现 AMQP (Adva ...
- QML 移动端适配一个参考思路
参考: Qt Quick 准确的移动平台屏幕适配 qt qml 高宽自动适配android设备 QML 从无到有 (移动适配) 思路:以一个平台分辨率为基准(如320*480),考虑其与其它平台的比例 ...
- 分库分表的情况下生成全局唯一的ID
分库分表情况下 跨库的问题怎么解决? 分布式事务怎么解决? 查询结果集集合合并的问题? 全局唯一的id怎么解决? 一般要求:1.保证生成的ID全局唯一,不可重复 2.生成的后一个Id必须大于前一个Id ...
- Shell基础 -Linux从入门到精通第九天(非原创)
文章大纲 一.关于shell二.shell进阶(重点)三.学习资料下载四.参考文章 一.关于shell 1. 什么是shell 1.1 shell简介 Shell(外壳) 是一个用 C 语言编写 ...
- (原)Ubuntu连接远程服务器时connection reset by peer
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11086935.html 最近使用ubuntu通过ssh连接服务器时,由于密码错误,多次连接失败后,在 ...
- 网络编程(二)--TCP协议、基于tcp协议的套接字socket
一.TCP协议(Transmission Control Protocol 传输控制协议) 1.可靠传输,TCP数据包没有长度限制,理论上可以无限长,但是为了保证网络的效率,通常TCP数据包的长度不会 ...
- Java字符串——String深入
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10840495.html 一:字符串的不可变性 1.可变 与 不可变 辨析 Java中的对象按照创建后,对象的 ...
- construct2 入门
construct2特点 construct2是一款跨平台二维游戏开发引擎,不需要编码,通过定义各个部件和事件完成html5的游戏开发.该引擎可以将开发的封装成多种形式,如phonegap.cocoo ...
- dapi 基于Django的轻量级测试平台三 接口关联
QQ群: GitHub:https://github.com/yjlch1016/dapi 一.接口关联思路: 在接口测试中, 很多场景下, 上一个接口的出参要作为下一个接口的入参, 即上一个接口的响 ...
- Python 文件读写操作实例详解
Python提供了必要的函数和方法进行默认情况下的文件基本操作.你可以用file对象做大部分的文件操作 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前 ...