poj1258 Agri-Net 最小生成树
来源:互联网 发布:java短链接 编辑:程序博客网 时间:2024/05/16 14:57
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 54785 Accepted: 22746
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
做2485送1258.。。模版题
#include <iostream>#include <string>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#include <cmath>using namespace std;const int maxn = 110;int n,m;int father[maxn];struct unit { int from,to; long long cost;}save[maxn*maxn];bool compare(unit a,unit b){ return a.cost<b.cost;}int find(int x){ if(x==father[x]) return x; return father[x] = find(father[x]);}bool isConnected(int a,int b){ return find(a) == find(b);}void mix(int a,int b){ int u = find(a),v = find(b); if(u==v){ return ; } father[u] = v;}long long Kruskal(){ long long result = 0; sort(save+1, save+1+m, compare); for(int i =1 ;i<=m;i++){ if(isConnected(save[i].from, save[i].to)){ continue; } mix(save[i].from, save[i].to); result += save[i].cost; } return result;}int main(){ while(~scanf("%d",&n)){ int i,j; for(i=1;i<=n;i++){ father[i]=i; } int a; m = 0; for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ scanf("%d",&a); save[++m].from = i; save[m].to = j; save[m].cost = a; } } bool flag = true; long long result = Kruskal(); printf("%lld\n",result); } return 0;}
0 0
- poj1258 Agri-Net最小生成树问题
- POJ1258 - Agri-Net - 最小生成树
- POJ1258 Agri-Net 【最小生成树Prim】
- poj1258--Agri-Net(最小生成树)
- POJ1258 Agri-Net(最小生成树)
- [poj1258 Agri-Net]最小生成树
- POJ1258 Agri-Net(最小生成树)
- POJ1258 Agri-Net 最小生成树
- poj1258 Agri-Net(最小生成树)
- POJ1258 Agri-Net(最小生成树)
- poj1258 Agri-Net 最小生成树
- poj1258 Agri-Net 最小生成树
- poj1258 Agri-Net (最小生成树prime)
- POJ1258,Agri-Net,明显的最小生成树
- POJ1258 Agri-Net MST最小生成树题解
- poj1258 Agri-Net 最小生成树,kruskal算法
- POJ1258——Agri-Net(最小生成树)
- 【北大 夏令营笔记-最小生成树】poj1258-Agri-Net
- 机器学习方法:使用Apriori算法和FP-growth算法进行关联分析(Python版)
- 数组中两个字符串的最小距离
- Destroy the Well of Life HDU - 1692
- Android Studio插件整理
- wpf数据库连接登陆案例
- poj1258 Agri-Net 最小生成树
- Eclipse中编写Java调用C代码
- 观察者模式
- C# using 三种使用方式
- 二维码操作工具类
- nginx+php+gitblog
- Android LayoutInflater详解
- Header V3 DSA signature: NOKEY, key ID***** 错误解决、依赖包解决方法
- 史上最全的idea快捷方式