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. es严格模式、对象和扩展。

    01. 严格模式 1. 理解: * 除了正常运行模式(混杂模式),ES5添加了第二种运行模式:"严格模式"(strict mode). * 顾名思义,这种模式使得Javascrip ...

  2. iOS7的十个更“佳”:简洁直观更受青睐

    转自:http://www.25pp.com/news/news_27792.html iOS7自发布以来一直是褒贬不一,虽然苹果还只是发布了第二个测试版,但普通用户早已经在纠结到底该不该升级iOS7 ...

  3. 在ubuntu更新时,出现错误E: Some index files failed to download, they have been ignored, or old ones used inst

    原文:https://blog.csdn.net/tian_ciomp/article/details/51339635 在ubuntu更新时,出现错误E: Some index files fail ...

  4. DF1协议简述

    DF1协议 1.    概述 可编程控制器(PLC)因编程方便,抗干扰能力强,被广泛应用于各种领域.DF1协议是AB公司可编程控制器系统广泛支持的数据链路层通信协议,各系列可编程控制器及装有RSLin ...

  5. CentOS6安装部署配置Nginx

    操作系统: CentOS 6.9 脚本语言: shell https://github.com/iscongyang/Practical/blob/master/shell-scripts/insta ...

  6. 最常见Linux操作

    命令 含义 cd /home/hadoop #把/home/hadoop设置为当前目录 cd .. #返回上一级目录 cd ~ #进入到当前Linux系统登录用户的主目录(或主文件夹).在 Linux ...

  7. PHP随机获取预设的值

    前面我们讲了php怎么获取随机数,<?php echo rand(1000,2000); ?> 一行代码就能搞定,如果要获取ASP,PHP,JAVASCRIPT,AJAX,CSS,JQUE ...

  8. ajax中什么时候进success和error

    先简单介绍下ajax: 简单的说Ajax请求通过XMLHttpRequest对象发送请求,该对象有四个状态(readyState): -未初始化.-正在初始化.-发送数据.-正在发送数据.-完成. 当 ...

  9. 自用ftp上传脚本

    #!/bin/sh backupserver=$1 #localdir=ftp_result_tmp username=$3 password=$4 #remodir='./Log/2018-01-2 ...

  10. ESP8266 LUA脚本语言开发: 测试下诱人的程序

    前言 这一节测试一下诱人的程序 实现的功能,APP通过SmartConfig给Wi-Fi模块配网并绑定设备,然后通过MQTT远程控制开发板的继电器, APP显示ESP8266采集的温湿度数据. 简而言 ...