prime 算法 POJ 1258
来源:互联网 发布:mac预览 编辑:程序博客网 时间:2024/06/02 03:26
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 35695 Accepted: 14353
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
最近在研究算法的一些相关问题 , 表示图论很坑 ,很多很难 在学校参加校赛,老是被计院的大神鄙视 , 是的我是外专业的学生,没有受到过专业的系统 的ACM 培训,很多知识都是通过他人的博客学来的 ,但是我可不会照搬 , 一定是在理解的基础上 ,加上自己的一些想法,然后自己敲一遍代码的,菜鸟要进步,每天每天进步, 最终进入最高等学府攻读计算机专业硕士学位!
言归正传:
Prime 算法:
先随便找到 放置在顶点集合 U当中其他顶点 为集合 V — U 当中 一个定点V1 然后找到和这个顶点链接的边最小权值的那个边
也就是说着到 与红线相交的线的最小权值的那边 即权值为1 的边 找到结点 V3
接着往下找 结点 : 即如图所示
即找到与红线相交的最小权值的边 因此着到结点 V6 接着往下找, 知道所有的点找完为止
下面举个例子 : 这里说明一下 图来于 其他大神博主 , 小菜鸟膜拜的紧,这里特别表示感谢
看例子应该能明白求最小路的算法Prime 其实就是不断的取小的权值的边
下面给出 POJ1258 代码:
#include<iostream>using namespace std;int main(){ int a[101][101]; int n ; while(cin>>n) { int i , j; for(i = 0 ; i < n ; i++) { for(j = 0 ; j < n ; j++) { cin>>a[i][j]; //输入需要计算的值 这里采用相邻矩阵来存储图 } } int visited[101] = {0}; int sum = 0; visited[0] = 1; int k ; for(k = 0 ; k < n ; k++) { int min = 1000001; int pos ; for(i = 0 ; i < n ; i++ ) { if(!visited[i] && a[0][i] < min) { //找到 1 所对应的最小的边 min = a[0][i]; pos = i; } } visited[pos] = 1; sum += a[0][pos]; //下面开始更新最小值 for(j = 0 ; j < n ; j++) { if(!visited[i] &&a[0][j] > a[pos][j]) {//其实下面的j是新的结点 下一次循环后也就是再经过上面的循环 j 有会赋值给pos //因此保证每次加的都是最小值 a[0][j] = a[pos][j]; } } } cout<<sum<<endl; } return 0;}
0 0
- prime 算法 POJ 1258
- POJ 1258 Agri-Net 【MST,Prime算法】
- poj 1258 Agri-Net(最小生成树 prime算法)
- POJ 1287 Networking(prime算法)
- poj 2421-Constructing Roads(经典Prime算法)
- poj 1811 Prime Test_Pollard_rho算法模板
- POJ 2560 Freckles Prime算法题解
- POJ 3126 Prime Path(BFS算法)
- poj 2485 Highways(prime算法模板)
- poj 1287(最小生成树prime算法)
- Agri-Net(POJ 1258)(裸最小生成树)(Prime算法+Kruskal算法)
- [POJ 1811]Prime Test---Miller-Rabin算法&Pollard-rho算法
- Prime算法
- Prime算法
- Prime算法
- Prime算法
- prime算法
- Prime算法
- 问题 D : 植树都费劲
- RTP协议分析
- 我的第一篇博客
- 蓝色茉莉
- php表单的交互(post方法)
- prime 算法 POJ 1258
- spring 加载xml机制
- 问题 E : 31
- 黑马程序员- 第二天(标识符,常量,变量,运算符,转义字符,语句)
- GPU介绍,图片格式、纹理、size
- Marvell面试经历 ZZ
- IOS项目开发布局二
- 文件夹不存在则创建
- VxWorks workbench中simulator的使用心得