POJ 1258Agri-Net(最小生成树)
来源:互联网 发布:软件项目评审表模板 编辑:程序博客网 时间:2024/06/05 05:33
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 54129 Accepted: 22477
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
prim算法
#include <iostream>#include <string.h>#include <stdio.h>using namespace std;const int Ink = 0X3f3f3f3f;int Map[150][150];int low[150];int main(){ int n, i, j, k, Min, Max, T; while (~scanf("%d", &n)) { for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { scanf("%d", &Map[i][j]); } } for(i = 0; i < n; i++) { low[i] = Map[0][i]; } int sum = 0; for(i = 0; i < n - 1; i++) { Min = Ink; for(j = 0; j < n; j++) { if(low[j] && low[j] < Min) { Min = low[j]; k = j; } } low[k] = 0; sum += Min; for(j = 0; j < n; j++) { if(low[j] > Map[k][j]) low[j] = Map[k][j]; } } printf("%d\n",sum); } return 0;}
0 0
- POJ 1258 Agri-Net(最小生成树)
- POJ 1258 Agri-Net 【最小生成树】
- Agri-Net - POJ 1258 最小生成树
- 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 1258Agri-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 最小生成树
- angualr使用技巧
- RxJava之入门详解
- Linux设备驱动程序加载/卸载方法汇总
- Cookie/Session机制详解
- CentOS学习18_关于CentOS 6下Hadoop占用系统态CPU高的处理办法
- POJ 1258Agri-Net(最小生成树)
- 如何强制ffmpeg编码时输出一个关键帧
- Linux编程VIM编辑器的命令模式
- android去掉滑动到顶部和底部的阴影
- coredata学习总结(十四)
- MySQL自定义函数
- 实用的正则
- LeetCode 448. Find All Numbers Disappeared in an Array
- 霍夫曼树简介