poj 1258(最小生成树 Kruskal)
来源:互联网 发布:行业研究报告 知乎 编辑:程序博客网 时间:2024/05/02 03:01
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 26390 Accepted: 10410
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
Source
USACO 102
题目类型:最小生成树
题目描述:略
题目分析:把N个节点连接到一起,要求权值最小。这样的问题,就属于最小生成树的问题。
相当于,在无向连通图中,找一无回路子图,这个子图连接了所有的定点,并且所用权值最小。
因为是无回路图且连接了所有定点,所以肯定是树,称为生成树,因为权值最小,所以称最小生成树。
最小生成树算法的思想是,维护一个边集A,A为图G某最小生成树边集的子集。
然后,往A中加入安全边直到,A为图G的某个最小生成树。
如果把边(u,v)加入到A中, A仍然能保持原来的性质(为某最小生成树边集的子集),那么就称(u,v)为安全边。
对于Kruskal算法来说,他是这么选择安全边的。
(1) 把每个顶点作为一个集合。
(2) 把图中的边按升序排序。
(3) 遍历图中的边(u,v),如果u和v不在一个集合里。此时(u,v)为安全边。把u,v所在集合合并,把(u,v)加入到A中。
代码如下:
#include <iostream>#include <stdio.h>#include <algorithm>#define V 101#define E V * (V-1) / 2using namespace std;struct Edge{ int u; int v; int w;};Edge edge[E];int f[V];int rank[V];int n;bool cmp(Edge a,Edge b){ if( a.w < b.w) { return true; } else { return false; }}void makeSet(){ for(int i = 0; i < n; i++){ f[i] = i; rank[i] = 0; }}int findRoot(int x){ if(x == f[x]){ return x; } else { return f[x] = findRoot(f[x]); }}void merge(int a,int b){ int ra = findRoot(a); int rb = findRoot(b); if(ra != rb) { if(rank[ra] < rank[rb]){ f[ra] = rb; } else { f[rb] = ra; if(rank[ra] == rank[rb]){ rank[ra]++; } } }}int main(){ while(scanf("%d",&n) != EOF){ int sum = 0; int k = -1; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ int w; scanf("%d",&w); if( j > i){ k++; edge[k].u = i; edge[k].v = j; edge[k].w = w; } } } //Kruskal makeSet(); sort(edge,edge+k+1,cmp); for(int i = 0; i <= k; i++){ int ru = findRoot(edge[i].u); int rv = findRoot(edge[i].v); if(ru != rv) { sum += edge[i].w; merge(edge[i].u,edge[i].v); } } printf("%d\n",sum); } return 0;}
- poj 1258(最小生成树 Kruskal)
- POJ 1258 (最小生成树 kruskal)
- poj-1258-最小生成树kruskal模板
- Poj 1258 Agri-Net (最小生成树Prim & Kruskal)
- poj 1258 Agri-Net 最小生成树 kruskal
- POJ 1258 Agri-Net(最小生成树 Kruskal)
- POJ 1258 Agri-Net (kruskal求最小生成树)
- POJ 1258--Agri-Net【最小生成树 && Kruskal && 水题】
- POJ 1258 Agri-Net 最小生成树 prim && kruskal
- Poj 1258 Agri-Net(最小生成树 kruskal)
- POJ 1258(最小生成树之Kruskal)
- POJ 1258 Agri-Net(最小生成树Kruskal算法)
- poj 1258 Agri-Net 【最小生成树-Kruskal】
- 最小生成树Kruskal算法,poj 2075
- poj 2485Highways(最小生成树 Kruskal)
- poj 2253 最小生成树 kruskal
- poj 2263 kruskal最小生成树
- poj 1861 Network (kruskal 最小生成树)
- android内存查看的几个命令
- LINUX常用命令(基础)
- WF & WCF(4)
- Java Reflect 机制
- Linux下FTP命令详解
- poj 1258(最小生成树 Kruskal)
- java reflect
- zoj1076 Gene Assembly 活动安排 dp
- 2012年 5月目标
- Linux 打包解包压缩解压缩命令
- Javassist字节码增强
- 解决SQL SERVER2005不能通过IP连接
- 《出口成章 - 敏捷思考,完美表达》学习心得
- Alpha服务器常见问题解答