POJ1258,Agri-Net,明显的最小生成树
来源:互联网 发布:2016高仿耐克淘宝店 编辑:程序博客网 时间:2024/05/16 18:25
Agri-Net
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
分析:
Prim不解释
code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#define MAX 100#define INF 999999using namespace std;int N,dis[MAX+5][MAX+5];int prim(){ int s=1,num=1,sum_w=0,min_w,flag,low_dis[MAX+10]; bool uni[MAX+10]; memset(uni,false,sizeof(uni)); for(int i=1;i<=N;i++) low_dis[i]=999999; uni[s]=true; while(1) { if(num==N) break; min_w=999999; for(int i=2;i<=N;i++) { if(!uni[i]&&dis[s][i]<low_dis[i]) low_dis[i]=dis[s][i]; if(!uni[i]&&low_dis[i]<min_w) { min_w=low_dis[i]; flag=i; } } uni[flag]=true; s=flag; num++; sum_w+=min_w; } return sum_w;}int main(){ //freopen("input.txt","r",stdin); while(scanf("%d",&N)!=EOF) { for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) scanf("%d",&dis[i][j]); printf("%d\n",prim()); } return 0;}
- POJ1258,Agri-Net,明显的最小生成树
- 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 MST最小生成树题解
- poj1258 Agri-Net 最小生成树,kruskal算法
- POJ1258——Agri-Net(最小生成树)
- 【北大 夏令营笔记-最小生成树】poj1258-Agri-Net
- while循环中,用scanf输入一个float型的数字,如何让判断输入的是否为数字...
- 项目开发的五个阶段
- python re模块 笔记
- hdu 2034 人见人爱A-B
- 修正版Sqlserver
- POJ1258,Agri-Net,明显的最小生成树
- 生成正态分布及泊松分布的随机数
- 程序员面试题精选100题(29)-调整数组顺序使奇数位于偶数前面
- Google常见使用技巧
- PHP操作MongoDB
- ubuntu系统各个分区的作用
- android五大布局对象
- 动态数据的国际化、几种主要的国际化标签
- 转 CSS的组合和嵌套 id class, 点, #