最小生成树 --- 求最小权值、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 ...
随机推荐
- Bootstrap初始化过程源码分析--netty客户端的启动
Bootstrap初始化过程 netty的客户端引导类是Bootstrap,我们看一下spark的rpc中客户端部分对Bootstrap的初始化过程 TransportClientFactory.cr ...
- 《linux就该这么学》课堂笔记20 LNMP架构部署动态网站环境
源码包安装程序 使用源码包来安装服务程序具有两个优势. 源码包的可移植性非常好,几乎可以在任何Linux系统中安装使用,而RPM软件包是针对特定系统和架构编写的指令集,必须严格地符合执行环境才能顺利安 ...
- Linux服务器安全加固
关于对公司网站服务器安全加固的一些想法及思路: 一.修改密码和ssh登录端口,并且尽可能的用密钥对登录,禁止用密码登录(主要针对Linux)二.修改/etc/hosts.allow 设置仅仅允许某几台 ...
- json转dataframe格式
方法1:利用pandas自带的read_json直接解析字符串 方法2:利用json的loads和pandas的json_normalize进行解析 方法3:利用json的loads和pandas的D ...
- js 驼峰命名和下划线互换
代码走你 // 下划线转换驼峰 function toHump(name) { return name.replace(/\_(\w)/g, function(all, letter){ return ...
- 在windows下安装Superset
前言 最近想用一下Superset,这个是一个开源项目,可以直接通过写sql来生成图表,有时候对一些图表需求比较多的时候,可以用的上. Superset是由Airbnb(知名在线房屋短租公司)开源BI ...
- 201871010110-李华《面向对象程序设计(java)》第四周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- hdu1005-Number Sequence-(循环节)
题意:已知f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7,给出A,B,n,求f(n) 题解:n巨大,循环肯定超时,在模7的 ...
- Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)
- three.js 居中-组
原文:https://blog.csdn.net/qq_30100043/article/details/78921224 代码: <!DOCTYPE html> <html lan ...