POJ1258 Kruskal
来源:互联网 发布:node 虚拟主机 编辑:程序博客网 时间:2024/06/05 14:15
Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 47203 Accepted: 19526
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
题解:
求这个网络里的连接各个节点的最小花费。
题解:
生成树裸题。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <queue>#include <vector>#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)using namespace std;struct Edge{ int from,to,v;};int n,cnt;Edge edge[300000];int p[1000];int High[1000];bool cmp(Edge a,Edge b){ if(a.v<b.v) return true; else return false;}int Find(int k){ if(p[k] == k) return k; return p[k] = Find(p[k]);}int Kruskal(){ int i,j,sum = 0; sort(edge,edge+cnt,cmp); f(i,1,n) {p[i] = i;High[i] = 1;} f(i,0,cnt-1){ int root1 = Find(edge[i].from); int root2 = Find(edge[i].to); if(root1 == root2) continue; sum += edge[i].v; if(High[root1]>High[root2]){ p[root2] = root1; High[root1] += High[root2]; }else{ p[root1] = root2; High[root2] += High[root1]; } } return sum;}int main(){ int N; while(~scanf("%d",&n)&&n){ int i,j; cnt = 0; f(i,1,n) f(j,1,n){ int k; scanf("%d",&k); if(i == j) continue; edge[cnt].from = i;edge[cnt].to = j;edge[cnt++].v = k; } printf("%d\n", Kruskal()); } return 0;}
- POJ1258 Kruskal
- POJ1258--Agri-Net--Kruskal
- POJ1258 Agri-Net,Kruskal算法
- POJ1258《Agri-Net》方法:Kruskal
- Agri-Net poj1258||Kruskal模板
- 最小生成树poj1258 prim和kruskal
- POJ1258-最小生成树-kruskal模板
- 【poj1258】Agri-Net——Kruskal
- poj1258(最小生成树 kruskal算法)
- poj1258
- poj1258
- POJ1258
- poj1258
- poj1258
- poj1258
- poj1258
- poj1258
- POJ1258
- Scala Everything is an object
- B-树小结汇总
- Regular Expression - Posix
- Linux使用笔记
- 欢迎使用CSDN-markdown编辑器
- POJ1258 Kruskal
- mysql sql语句大全
- 南邮编程在线编程题十二:八进制转换为二进制
- BestCoder Round #65 1002 ZYB's Game HDU5591
- Android网络请求
- js学习,变量提升(hositing)与作用域(Scoping)浅析
- 8、深入理解Looper、Handler、Message
- HackerRank Largest Rectangle
- 黑马程序员——面向对象_static关键字