Agri-Net(最小生成树prime)
来源:互联网 发布:windows 8.1 10 whql 编辑:程序博客网 时间:2024/05/18 02:45
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 54037 Accepted: 22434
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
简单的最小生成树
prime
#include <iostream>#include <cstdio>#include <cstring>#define inf 0x3f3f3f3fusing namespace std;int mp[150][150],vis[150],dis[150];int main(){ int n,i,j; while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) mp[i][j]=0; else mp[i][j]=inf; } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&mp[i][j]); } } for(i=1;i<=n;i++) { dis[i]=mp[1][i]; } vis[1]=1; int count=1,ans=0; while(count<n) { int min=inf,t; for(i=1;i<=n;i++) { if(!vis[i]&&dis[i]<min) { min=dis[i]; t=i; } } vis[t]=1; count++; ans+=min; for(j=1;j<=n;j++) { if(!vis[j]&&dis[j]>mp[t][j]) { dis[j]=mp[t][j]; } } } cout<<ans<<endl; } return 0;}
0 0
- Agri-Net(最小生成树prime)
- poj1258 Agri-Net (最小生成树prime)
- POJ-1258--Agri-Net (最小生成树 Prime)
- poj 1258 Agri-Net(最小生成树 prime算法)
- POJ 1258 Agri-Net(最小生成树--prime 水)
- USACO Section 3.1 Agri-Net(最小生成树Prime算法)
- USACO 3.1.1 Agri-Net 最短网络(最小生成树Prime算法)
- poj1258 Agri-Net +hdu 1233 还是畅通工程 (最小生成树Prime算法)
- Agri-Net(POJ 1258)(裸最小生成树)(Prime算法+Kruskal算法)
- POJ 1258 Agri-Net 【最小生成树入门题目】【prime模板】
- POJ 1258 Agri-Net(最小生成树)
- USACO Agri-Net,最小生成树
- POJ 1258 Agri-Net 【最小生成树】
- Agri-Net - POJ 1258 最小生成树
- POJ 1258 Agri-Net 最小生成树
- POJ 1258 Agri-Net(最小生成树)
- POJ 1258 Agri-Net 最小生成树
- poj1258 Agri-Net最小生成树问题
- 2016美国大选
- 机房那些事儿---不解释之机房知识(各种表)
- 二叉树的层序遍历(非递归)
- c++如何求任意多边形的面积
- 真正从零开始,TensorFlow详细安装入门图文教程!
- Agri-Net(最小生成树prime)
- codevs 4175 收费站(二分+spfa)
- 【java】三种方法从键盘输入到终端
- JVM性能调优监控工具
- Android Handler机制
- 疑难总结
- 最长回文子串
- 算法-插入排序
- 应用程序组件-------Activity之AppBar(二)