[POJ] 1258 Agri-Net
来源:互联网 发布:linux bond 模式 编辑:程序博客网 时间:2024/05/16 00:37
题目:
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<algorithm>using namespace std ;int a[200][200];int f[200];class edge{ public: int u ; int v ; int w ; };int getf(int x){ if(x==f[x]) return x ; else return f[x] = getf(f[x]); }int merge(int a,int b){ int tx = getf(a) ; int ty = getf(b) ; if(tx!=ty){ f[ty] = tx ; return 1 ; } else return 0 ;}bool compare(edge a, edge b ){ if(a.w<b.w) return true ; else return false ; }int main(){ //freopen("in.txt","r",stdin); int n ; edge e[20000]; while(scanf("%d",&n)!=EOF){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++) scanf("%d",&a[i][j]); } int num = 0 ; for(int i = 1 ; i <= n-1 ; i++){ for(int j = i+1 ; j <= n ; j++){ e[++num].u = i ; e[num].v = j ; e[num].w = a[i][j] ; } } long long sum = 0 ; int count = 0 ; sort(e+1,e+1+num,compare); for(int i = 1 ; i <= n ; i++) f[i] = i ; for(int i = 1 ; i <= num ; i++ ){ if(merge(e[i].u,e[i].v)){ sum += e[i].w; count ++ ; } if(count == n-1)break ; } printf("%lld\n",sum); } return 0 ;}
- 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 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 1258 Agri-Net
- POJ 1258 - Agri-Net
- 关于JAVA中GUI开发(按钮数组的添加)
- oracle物化视图
- 小马哥----高仿苹果7 主板S89P 芯片为6572刷机拆机主板图与开机识别图
- javaScript 正则表达式
- bzoj4767: 两双手
- [POJ] 1258 Agri-Net
- 手动添加jar包到本地仓库
- 常用sql记录
- Spring 知识点总汇
- iOS10 添加本地推送(Local Notification)
- oc中的oop基础及类的基本介绍
- 欢迎使用CSDN-markdown编辑器
- 数据库管理(ORC/SQL)(258)
- scala-json-json4s-工具类