Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37109   Accepted: 14982

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
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

The
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

For
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

【题目来源】

USACO 102

【题目大意】

给定一个强连通图,让你求最小生成树的权值之和。

【题目分析】

数据很水,用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的更多相关文章

  1. POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

    题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...

  2. poj3565 Ants km算法求最小权完美匹配,浮点权值

    /** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...

  3. ZOJ-2342 Roads 二分图最小权值覆盖

    题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...

  4. poj 3565 uva 1411 Ants KM算法求最小权

    由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...

  5. POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)

    题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...

  6. POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你 ...

  7. POJ 2195 Going Home 【二分图最小权值匹配】

    传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  8. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  9. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

随机推荐

  1. 并发编程-线程,JMM,JVM,volatile

    1.线程 相信大家对线程这个名词已经很不陌生了,从刚开始学习java就接触到线程,先说说进程吧,进程就是系统分配资源的基本单位,线程是调度cpu的基本单位,进程由线程组成,一个进程至少又一个线程组成, ...

  2. Qt json使用

    JSON 6 种基本数据类型 QJsonValue::Bool QJsonValue::Double QJsonValue::String QJsonValue::Array QJsonValue:: ...

  3. 设计模式之代理模式(proxy pattern)

    代理模式的本质是一个中间件,主要目的是解耦合服务提供者和使用者.使用者通过代理间接的访问服务提供者,便于后者的封装和控制.是一种结构性模式. 1.目的 为外部调用者提供一个访问服务提供者的代理对象. ...

  4. Hashtable,HashMap和ConcurrentHashMap的原理及区别

    一.原理 Hashtable 底层数组+链表实现,无论key还是value都不能为null,线程安全,实现线程安全的方式是在修改数据时锁住整个HashTable,效率低,ConcurrentHashM ...

  5. Windows 2016 & Windows 10 中IIS安装和配置PHP的步骤

    Windows 2016 和 Windows 10 内核是相同的,我们首先需要安装 Internet Information Services (IIS),当然 Win2016 跟 Win10 安装  ...

  6. django 基础进阶ORM COOKIE

    ORM: class Book(models.Model): title=models.CharFiled(max_length=32) 类-----------------表    #  Book- ...

  7. Spring Boot 注入外部配置到应用内部

    Spring Boot允许你外部化你的配置,这样你就可以在不同的环境中使用相同的应用程序代码,你可以使用properties文件.YAML文件.环境变量和命令行参数来外部化配置,属性值可以通过使用@V ...

  8. python基础之函数重点

    函数的返回值 现在有一个需求,比较两个人的月薪,然后想获取月薪较大人的年薪. 如果需要在程序中拿到函数的处理结果做进一步的处理,则需要函数必须要有返回值. 需要注意的是: return是一个函数结束的 ...

  9. 【JZOJ6236】【20190628】启程的日子

    题目 给你一个\(n \times m\)的01矩阵 你需要用一些矩阵加减出这个矩阵 求最少的步数,并输出方案 需要满足构造出的01矩阵是一个四联通块 $ n ,  m \le 500 $ 题解 答案 ...

  10. 【CF573E】Bear and Bowling

    [CF573E]Bear and Bowling 题面 洛谷 题解 首先有一个贪心的结论: 我们一次加入每个数,对于\(\forall i\),位置\(i\)的贡献为\(V_i = k_i\times ...