[ACM] poj 1258 Agri-Net (最小生成树)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 37131 | Accepted: 14998 |
Description
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
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
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 (最小生成树)的更多相关文章
- POJ 1258 Agri-Net (最小生成树)
Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...
- POJ 1258 Agri-Net(最小生成树,模板题)
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- POJ 1258 Agri-Net(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...
- poj 1258 Agri-Net【最小生成树(prime算法)】
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44827 Accepted: 18351 Descri ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- 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 ...
- 最小生成树 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 // 顶点的最大个数 ( ...
- 北大ACM - POJ试题分类(转自EXP)
北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...
随机推荐
- HDU4850 构造一个长度n串,它需要随机长度4子是不相同
n<=50W.(使用26快报) 构造函数:26一个.截至构建26^4不同的字符串,最长的长度26^4+3.如此之大的输出"impossble",被判重量的四维阵列. 在正向结 ...
- asp.net2.0安全性(4)--Login系列控件--转载来自车老师
前面主要说了与安全相关的一系列的类,现在我们使用这些类就可以做出我们自己的安全系统了.其实微软的目的远不至于此,下面我们就来看一下微软为我们提供的Login系列控件. Login系列控件是微软为了简化 ...
- js显示时间
function nowTime(){ var data= new Date(); var y=data.getFullYear(); var m=parseInt(data.getMonth())+ ...
- 基于visual Studio2013解决面试题之0604O(1)时间复杂度删除链表节点
题目
- 关于Smartforms换页的
smartforms中有系统变量SFSY-PAGE是总页码,SFSY-FORMPAGES是当前页,可以最后的窗体中做个判断 1.把窗体设置成最终窗体 2.新增一个命令,当前页等于最后一页才输出改内容, ...
- Ubuntu 环境安装整理
Ubuntu11.04下Java开发环境搭建和配置 转自:http://guoyunsky.iteye.com/blog/1175861 类似的搭建,网上一搜一大把,但每次去搜索比较麻烦.我这里就整理 ...
- heap表和iot表排序规则不同
heap 和iot 对比 OBJECT_NAME OBJECT_TYPE --------------------------------------------------------------- ...
- javascript json格式解析方法
json.parse用于从一个字符串中解析出json对象 stringify()用于从一个对象解析出字符串 var dataObj = eval("("+json+")& ...
- FOJ 1607 Greedy division 数学题
题目地址: http://acm.fzu.edu.cn/problem.php?pid=1607 给定一个n,将n平均分成m份,问有几种方法,每种方法中找出最大的数.思路:就是求n的因子数.先将每个数 ...
- Lucene.Net 2.3.1开发介绍 —— 二、分词(六)
原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(六) Lucene.Net的上一个版本是2.1,而在2.3.1版本中才引入了Next(Token)方法重载,而ReusableStrin ...