poj 1258 最小生成树 prim
来源:互联网 发布:Windows切换目录 编辑:程序博客网 时间:2024/06/05 05:27
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 45448 Accepted: 18692
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
#include<iostream>#include<cstdio>#include<cstring>#define INF 0x3f3f3f3fusing namespace std;int edge[105][105];int n;int next[105],low[105];int Prim(int u0){int sum=0;for(int i=0;i<n;i++){low[i]=edge[u0][i];next[i]=u0;}next[u0]=-1;for(int i=0;i<n-1;i++){int Min=INF;int v=-1;for(int j=0;j<n;j++){if(next[j]!=-1&&Min>low[j]){Min=low[j];v=j;}}if(v!=-1){next[v]=-1;sum+=low[v];for(int j=0;j<n;j++){if(next[j]!=-1&&low[j]>edge[v][j]){low[j]=edge[v][j];next[j]=v;}}}}printf("%d\n",sum);}int main(){while(~scanf("%d",&n)){for(int i=0;i<n;i++)for(int j=0;j<n;j++){scanf("%d",&edge[i][j]);}Prim(0);}}
1 0
- POJ 1258 : 最小生成树(Prim)
- poj 1258 prim最小生成树
- POJ-1258 最小生成树 prim算法
- poj 1258 prim最小生成树
- POJ 1258 Prim最小生成树
- poj 1258 最小生成树 prim
- POJ 1258 -- 最小生成树(prim算法)
- poj 1258 最小生成树Prim
- POJ 1258-Agri Net 最小生成树Prim算法
- poj 1258 最小生成树 prim+优先队列
- poj 1258Agri-Net-最小生成树prim
- Poj 1258 Agri-Net (最小生成树Prim & Kruskal)
- poj 1258 【最小生成树】【prim】【纯模板】
- POJ 1258 prim模版求最小生成树
- POJ 1258 Agri-Net 图论 prim算法 最小生成树
- POJ 1258 Agri-Net 最小生成树 Prim
- poj 1258 Agri-Net(prim求最小生成树)
- POJ 1258:Agri-Net(最小生成树&&prim)
- poj 2485 最小生成树中的最大边 prim
- Python爬虫学习记录(1)——百度贴吧图片下载
- BC-52-1001
- android-使用webview来开发混合应用
- UILabel
- poj 1258 最小生成树 prim
- Socket编程步骤
- hdu1014 Uniform Generator
- 我们被叫做90后 | 致青春(催泪好文,自备纸巾)
- 学习笔记 post和get的区别
- R语言之文本挖掘
- socket编程--socket基本概念
- hdu 1017 A Mathematical Curiosity
- 杭电ACM1425——sort~~水题