Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37131   Accepted: 14998

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. 

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

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

Source

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=5010;
int parent[110];
int n; struct Node
{
int from,to,edge;
}node[maxn]; void init(int n)
{
for(int i=1;i<=n;i++)
parent[i]=i;
} int find(int x)
{
return parent[x]==x?x:find(parent[x]);
} bool cmp(Node a,Node b)
{
if(a.edge<b.edge)
return true;
return false;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int m=1;
int len;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&len);
if(i<j)
{
int temp=m;
node[temp].from=i;
node[temp].to=j;
node[temp].edge=len;
m++;
}
}
init(n);
m=n*(n-1)/2;
len=0;
sort(node+1,node+1+m,cmp);
for(int i=1;i<=m;i++)
{
int x=find(node[i].from);
int y=find(node[i].to);
if(x==y)
continue;
else
{
len+=node[i].edge;
parent[x]=y;
}
}
printf("%d\n",len);
}
return 0;
}

prim算法:

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int maxn=110;
const int inf=0x7fffffff;
int map[maxn][maxn],low[maxn],visit[maxn];
//map数组用来记录地图,low数组用来保存与已增加树中的顶点相连的边。(保证权值最小的肯定在里边)。visit用来记录该顶点是否已增加树中
int n; int prim()
{
int pos,Min,result=0;
memset(visit,0,sizeof(visit));
visit[1]=1,pos=1;//首先找的是1这个顶点,增加图中
for(int i=1;i<=n;i++)
if(i!=pos)
low[i]=map[pos][i];//与1顶点相连的边地权值
for(int i=1;i<n;i++)//每次增加一条边。n个顶点增加n-1条边。循环n-1次就能够了
{
Min=inf;
for(int j=1;j<=n;j++)
if(!visit[j]&&Min>low[j])
{
Min=low[j];//Min找到的是与已增加图中的顶点相连的边的最小值
pos=j;//该顶点为j
}
result+=Min;//加上最小边
visit[pos]=1;//新的顶点位置
for(int j=1;j<=n;j++)
if(!visit[j]&&low[j]>map[pos][j])
low[j]=map[pos][j];//更新low[]值,新的与图中已有的点相连的边,假设比原来小的,就更新
}
return result;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
map[j][i]=map[i][j];
}
//对于题目中输入了map[i][i]的值的话。map数组不用预处理,假设没输入,那么memset(map,0x3f3f3f3f,sizeof(map));最大化处理
cout<<prim()<<endl;
}
return 0;
}

[ACM] poj 1258 Agri-Net (最小生成树)的更多相关文章

  1. POJ 1258 Agri-Net (最小生成树)

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

  2. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  3. POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)

    题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elec ...

  4. POJ 1258 Agri-Net(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...

  5. poj 1258 Agri-Net【最小生成树(prime算法)】

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44827   Accepted: 18351 Descri ...

  6. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  8. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  9. 北大ACM - POJ试题分类(转自EXP)

    北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...

随机推荐

  1. Java学习笔记九(泛型)

    1.介绍 所谓的泛型就是将类型作为一种參数来传递.有了泛型后类型不再是一成不变的.能够通过泛型參数来指定. 能够提供程序开发的灵活性. 2.泛型类或接口的使用 泛型类声明时.与普通类没有太大的差别,仅 ...

  2. 谈论Java原子变量和同步的效率 -- 颠覆你的生活

    我们认为,由于思维定式原子变量总是比同步运行的速度更快,我想是这样也已经,直到实现了ID在第一次测试过程生成器不具有在这样一个迷迷糊糊的东西. 测试代码: import java.util.Array ...

  3. Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)

    解题报告 二分图第一题. 题目描写叙述: 为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; ...

  4. 重新配置与卸载 11gR2 Grid Infrastructure

    Oracle 11g R2 Grid Infrastructure 的安装与配置较之前的版本提供了更多的灵活性.在Grid Infrastructure安装完毕前执行root.sh经常容易出现错误,并 ...

  5. 编译mapnik(win7 环境下vs2008编译mapnik 0.7.1 成功)

    编译mapnik(win7 环境下vs2008编译mapnik 0.7.1 成功) ------by  wangsh 2012.02.22 Mapnik 是一个开源的 Python/C++ 地图渲染引 ...

  6. HDU 3328 Flipper (stack)

    最近着手打基础,做做STL的题目,虽然一般STL题目难度不大,但需要加快速度的准确率............................. 本题有N张牌,一开始每个位置一张(正面朝上或者朝下),有 ...

  7. android用canvas绘制两种波纹效果

     波形效果有几种不同的呈现形式,比如从中间向四周散开的波形,也就是熟知的水涟漪:还有上下波动的曲线,像五线谱等.英文中可以称作Wave或者Ripple,所以暂且叫它们WaveView.WaveLayo ...

  8. UVA 839 (13.08.20)

     Not so Mobile  Before being an ubiquous communications gadget, a mobile wasjust a structure made of ...

  9. 利用iframe技巧获取訪问者qq

    今天工作时,有个暂时加的好友问我,怎么利用web页面获取訪问者的qq. 曾经没有接触过,感觉到非常好奇,可是工作中脑子非常亢奋,转的快,利用所学的知识迅速想到一条技巧,那就是假想用户在进入我们设定的页 ...

  10. Test oracle db iops

    Today, i need to test one database's iops and do something for oracle db's io test. How to test the ...