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

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
做题做的头疼,水一道:
#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f
#define MAX 110
int map[MAX][MAX],low[MAX],vis[MAX];
int t;
void prime()
{
int i,j,next,min,lowdis=0;
memset(vis,0,sizeof(vis));
for(i=1;i<=t;i++)
{
low[i]=map[1][i];
}
vis[1]=1;
for(i=1;i<t;i++)
{
min=inf;
for(j=1;j<=t;j++)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
next=j;
}
}
lowdis+=min;
vis[next]=1;
for(j=1;j<=t;j++)
{
if(!vis[j]&&low[j]>map[next][j])
{
low[j]=map[next][j];
}
}
}
printf("%d\n",lowdis);
}
int main()
{
int n,m,j,i,s;
while(scanf("%d",&t)!=EOF)
{
for(i=1;i<=t;i++)
{
for(j=1;j<=t;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=inf;
}
}
for(i=1;i<=t;i++)
{
for(j=1;j<=t;j++)
{
scanf("%d",&n);
map[i][j]=n;
}
}
prime();
}
return 0;
}

  

poj 1258 Agri-Net【最小生成树(prime算法)】的更多相关文章

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

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

  2. POJ2395 最小生成树 - Prime算法

    题目: Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description The ...

  3. 最小生成树 prime算法 UVALive - 6437

    题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...

  4. 最小生成树prime算法模板

    #include<stdio.h> #include<string.h> using namespace std; int map[505][505]; int v, e; i ...

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

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

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

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

  7. poj 1287 Networking【最小生成树prime】

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7321   Accepted: 3977 Descri ...

  8. hdoj 1233 还是畅通工程---最小生成树---prime算法

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1233 可以用Kruskal来做,不过当图的边比较稠密的时候用prime会更快一些. AC代码:296MS ...

  9. hdoj 1863 畅通工程 最小生成树---prime算法

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #inclu ...

随机推荐

  1. Cocos2dx边学边总结——开篇(一)

    Cocos2dx是一个很好的开源跨平台2d游戏引擎,我们都知道他底层是基于OpenGl ES的,OpenGl 是跨平台的. 正是得益于这点 Cocos2dx的显示部分可以很好的跨平台运作,笔者认为 未 ...

  2. Google HTML/CSS/JS代码风格指南

    JS版本参见:http://www.zhangxinxu.com/wordpress/2012/07/google-html-css-javascript-style-guides/ HTML/CSS ...

  3. header("Location:login.php")

    header("Location:login.php")应该注意的几个问题  header("Location:")作为php的转向语句.其实在使用中,他有几点 ...

  4. ARM开发板系统移植-----rootfs的制作

    前面两篇文章分别介绍了mini2440开发板上运行的bootloader和kernel,到这里系统启动后其实是停留在一个“僵死”的状态---无法挂载根文件系统. 这里将介绍如何制作一个根文件系统,并且 ...

  5. WPF提示框效果

    WPF提示框效果 1,新建WPF应用程序 2,添加用户控件Message 3,在Message中编写如下代码 <Border x:Name="border" BorderTh ...

  6. [转] 小tips: 使用&#x3000;等空格实现最小成本中文对齐 ---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4562 一.重见天日第 ...

  7. Struts面试笔记

    Struts2面试题1.struts2工作流程Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器 ...

  8. php几个比较高级的函数

    1.传递任意数量的函数参数 我们在.NET或者JAVA编程中,一般函数参数个数都是固定的,但是PHP允许你使用任意个数的参数.下面这个示例向你展示了PHP函数的默认参数: 1 2 3 4 5 6 7 ...

  9. WPF2D绘制图形方法

    我们先看看效果如何: xaml文件: <Window x:Class="WPF2D绘制图形方法.MainWindow" xmlns="http://schemas. ...

  10. 《C和指针》章节后编程练习解答参考——6.4

    <C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...