POJ - 1258 Agri-Net(最小生成树简单题)
来源:互联网 发布:空间谱估计理论与算法 编辑:程序博客网 时间:2024/05/21 09:00
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u
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.
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
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int mnx = 110;const int mxe = mnx*(mnx - 1) / 2;int map[mnx][mnx];struct edge{int u, v, c;bool operator <(edge& b)const{return c < b.c;}}e[mxe];int fa[mnx];int n, m;int find(int t){if (fa[t] != t) fa[t] = find(fa[t]);return fa[t];}int kruskal(){sort(e + 1, e + m + 1);for (int i = 1; i <= n; i++)fa[i] = i;int ret = 0;for (int i = 1; i <= m; i++){int u = e[i].u, v = e[i].v, c = e[i].c;u = find(u), v = find(v);if (u != v){fa[u] = v;ret += c;}}return ret;}int main(){while (scanf("%d", &n) != EOF){for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)scanf("%d", &map[i][j]);m = 0;int ans = 0;for (int i = 1; i <= n; i++)for (int j = i + 1; j <= n; j++){m++;e[m].u = i;e[m].v = j;e[m].c = map[i][j];}ans = kruskal();printf("%d\n", ans);}}
0 0
- POJ - 1258 Agri-Net(最小生成树简单题)
- POJ 1258Agri-Net 简单的最小生成树
- poj 1258 Agri-Net(简单最小生成树)
- POJ 1258 Agri-Net(最小生成树)
- POJ 1258 Agri-Net 【最小生成树】
- Agri-Net - POJ 1258 最小生成树
- POJ 1258 Agri-Net 最小生成树
- POJ 1258 Agri-Net(最小生成树)
- POJ 1258 Agri-Net 最小生成树
- POJ 1258 Agri-Net 最小生成树
- poj 1258 Agri-Net(最小生成树)
- poj 1258 Agri-Net 最小生成树
- Poj 1258 Agri-Net【最小生成树】
- poj 1258 Agri-Net 最小生成树
- POJ 1258Agri-Net 最小生成树
- POJ 1258 :Agri-Net(最小生成树)
- POJ 1258 Agri-Net (最小生成树)
- poj 1258 Agri-Net 最小生成树
- iBeacon 创建一个UUID
- mongo表id自动生成
- 【hdoj 1164】Eddy's research I
- 非ARC情况下,设置使用僵尸对象,系统自动报错
- 30款高质量的HTML5和CSS3响应式模板
- POJ - 1258 Agri-Net(最小生成树简单题)
- ReactiveCocoa学习笔记
- POJ-2942 Knights of the Round Table
- nginx配置性能优化welly版
- js取url的参数
- svn 命令行下常用的几个命令
- 编译OpenWRT过程问题解决
- HDU 4763 Theme Section
- windows 无法安装到这个磁盘,选中的磁盘具有MBR分区表。在EFI系统上,windows只能安装在GPT磁盘上