poj1258 Agri-Net 最小生成树
|
Agri-Net
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 Sample Output 28 Source |
#include<stdio.h>
#include<string.h>
int map[105][105];
int dis[105],vis[105];
int n;
#define inf 999999999
int prim(int u){
int sum=0;
for(int i=1;i<=n;i++){
dis[i]=map[u][i];
}
vis[u]=1;
for(int i=1;i<n;i++){
int tmin=inf;
int temp;
for(int j=1;j<=n;j++){
if(dis[j]<tmin&&!vis[j]){
tmin=dis[j];
temp=j;
}
}
sum+=tmin;
vis[temp]=1;
for(int k=1;k<=n;k++){
if(dis[k]>map[temp][k]&&!vis[k])
dis[k]=map[temp][k];
}
}
return sum;
}
int main(){
while(scanf("%d",&n)!=EOF){
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
memset(map,0,sizeof(map));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j]);
}
}
printf("%d\n",prim(1));
}
return 0;
}
poj1258 Agri-Net 最小生成树的更多相关文章
- Poj1258 Agri-Net (最小生成树 Prim算法 模板题)
题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- POJ1258 Agri-Net【最小生成树】
题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离. 思路: 又是一个最小生成树,因为 ...
- POJ1258 Agri-Net MST最小生成树题解
搭建一个最小代价的网络,最原始的最小生成树的应用. 这里使用Union find和Kruskal算法求解. 注意: 1 给出的数据是原始的矩阵图,可是须要转化为边表示的图,方便运用Kruskal,由于 ...
- POJ-1258 Agri-Net(最小生成树)
Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...
- 最小生成树 prime poj1258
题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...
- POJ1258 基础最小生成树
本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一 ...
- 最小生成树Prim poj1258 poj2485 poj1789
poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u ...
- POJ1258:Agri-Net(最小生成树模板题)
http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of hi ...
- POJ1258 (最小生成树prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46319 Accepted: 19052 Descri ...
- POJ1258最小生成树简单题
题意: 给你个图,让你求一颗最小生成树. 思路: 裸题,克鲁斯卡尔或者普利姆都行. #include<stdio.h> #include<algorithm&g ...
随机推荐
- php 字符串的一些操作,以便记忆
php 字符串的操作 trim($str,'特殊字符')-----去除字符串左右两边的字符,返回字符串 ltrim(),rtrim()--------------------左,由两边,与trim() ...
- The first gui program by Qt
#include<QApplication> #include<QPushButton> int main(int argc, char **argv) { QAppl ...
- zabbix修改密码
在我们刚刚安装好zabbix之后我们的管理用户是 Admin,密码是zabbix 所以我们肯定是要更改密码的,这个方法也适用于我们忘记管理员密码 首先我们登录mysql数据库 这里有一个zabbix库 ...
- .net 使用PowerShell获取电脑中的UUID
UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...
- publish_subscribe
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- jQuery插件开发模式
jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...
- Spring-事物配置
Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManager,从而能支持各种数据访问框架的事务 ...
- JAVA TIMER定时器
备注:类实现ServletContextListener,在web.xml配置,之后服务启动该定时器类自动加载 package com.leadlt.common.util; import java. ...
- 洛谷P1202 [USACO1.1]黑色星期五Friday the Thirteenth
题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...
- Vijos1056 图形面积
描述 桌面上放了N个平行于坐标轴的矩形,这N个矩形可能有互相覆盖的部分,求它们组成的图形的面积. 格式 输入格式 输入第一行为一个数N(1≤N≤100),表示矩形的数量.下面N行,每行四个整数,分别表 ...