poj 1258 Agri-Net【最小生成树(prime算法)】
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 44827 | Accepted: 18351 |
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
Output
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算法)】的更多相关文章
- POJ 1258 Agri-Net(最小生成树,模板题)
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...
- POJ2395 最小生成树 - Prime算法
题目: Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description The ...
- 最小生成树 prime算法 UVALive - 6437
题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...
- 最小生成树prime算法模板
#include<stdio.h> #include<string.h> using namespace std; int map[505][505]; int v, e; i ...
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- POJ 1258 Agri-Net (最小生成树)
Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...
- poj 1287 Networking【最小生成树prime】
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7321 Accepted: 3977 Descri ...
- hdoj 1233 还是畅通工程---最小生成树---prime算法
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1233 可以用Kruskal来做,不过当图的边比较稠密的时候用prime会更快一些. AC代码:296MS ...
- hdoj 1863 畅通工程 最小生成树---prime算法
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #inclu ...
随机推荐
- POJ 3280 Cheapest Palindrome(DP 回文变形)
题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...
- CentOS 6.4 64位 安装 apache-tomcat-6.0.43
下载 tomcat: 地址:http://mirrors.hust.edu.cn/apache/tomcat/tomcat-6/v6.0.43/bin/apache-tomcat-6.0.43.tar ...
- TCP与UDP网络编程总结(一)
(1):TCP网络编程 我们注意到服务端与客户端通信时是通过客户端的套接字相互通信的,那么服务端的套接字主要是干什么用的呢? TCP服务端设置监听套接字时 int listen(int sock,in ...
- 「译」如何正确学习JavaScript
原文:How to Learn JavaScript Properly 目录 不要这样学习JavaScript 本课程资源 1-2周(简介,数据类型,表达式和操作符) 3~4周(对象,数组,函数,DO ...
- 我的接口框架---框架函数文件common.php
<?php defined('JDHU') OR die('no allow access'); /** * 加载配置文件 */ function &get_config($replac ...
- Balsamiq Mockups
Balsamiq Mockups完全手册 2010.03.18 发布 48,066 浏览 什么是Balsamiq Mockups Balsamiq Mockups出自加利福尼亚州的Balsamiq工作 ...
- freeswitch 拨号时添加自定义变量
Using Channel Variables in Dialplan Condition Statements Channel variables can be used in conditions ...
- 2016021902 - linux解压缩命令
转载自:http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz 解压2:gzip -d Fil ...
- hg vs git :这个世界除了svn还有别的
最近想用版本控制软件来保存汉化文件,但又觉得SVN太麻烦,于是想到了最近较为流行的分布式版本控制工具.而Git和Mercurial(意思为水银的,于是经常缩写为Hg)自然是其中最为流行的工具.大名鼎鼎 ...
- PI数据库的使用-PI System Management Tools
1.PI连接管理器 2.标记搜索 3.当前值