poj 1287 Networking【最小生成树prime】
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 7321 | Accepted: 3977 |
Description
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
Output
Sample Input
1 0 2 3
1 2 37
2 1 17
1 2 68 3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32 5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12 0
Sample Output
0
17
16
26 继续水一道,prime模板题
#include<stdio.h>
#include<string.h>
#define MAX 110
#define INF 0x3f3f3f3f
int n,m;
int map[MAX][MAX],low[MAX];
bool vis[MAX];
void init()
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=i==j?0:INF;
}
void getmap()
{
int i,j,a,b,c;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
}
void prime()
{
int i,j,min,mindis=0,next;
for(i=1;i<=n;i++)
{
low[i]=map[1][i];
vis[i]=false;
}
vis[1]=true;
for(i=1;i<n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&min>low[j])
{
next=j;
min=low[j];
}
}
mindis+=min;
vis[next]=true;
for(j=1;j<=n;j++)
{
if(!vis[j]&&low[j]>map[next][j])
low[j]=map[next][j];
}
}
printf("%d\n",mindis);
}
int main()
{
while(scanf("%d",&n),n)
{
scanf("%d",&m);
init();
getmap();
prime();
}
return 0;
}
poj 1287 Networking【最小生成树prime】的更多相关文章
- POJ 1287 Networking (最小生成树)
Networking Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit S ...
- POJ 1287 Networking (最小生成树模板题)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds ...
- POJ.1287 Networking (Prim)
POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...
- POJ 1287 Networking (最小生成树)
Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...
- POJ 1287 Networking(最小生成树)
题意 给你n个点 m条边 求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...
- [kuangbin带你飞]专题六 最小生成树 POJ 1287 Networking
最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Creat ...
- POJ - 1287 Networking 【最小生成树Kruskal】
Networking Description You are assigned to design network connections between certain points in a wi ...
- POJ 1287 Networking(最小生成树裸题有重边)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
随机推荐
- UITableView编写可以添加,删除,移动的物品栏(二)
MyTableViewCell.h文件(自定义ViewCell)的内容: MyTableViewCell.m的内容
- 利用反射把查询到的Table、Reader转换成List、Model
菜鸟一枚,入园已有两年三个月,这还是第一次写博客,请各位大神斧正. 这是我写的一个工具类,通常我们从数据库查询到一个 DataReader 或者是 一个 Table , 想要转换成 一个 lis ...
- gif图简介
多媒体教程 - GIF 图 GIF 是在 Web 上使用的主要图像格式之一. 本文详细讲解了 GIF 图像的特性和使用技巧. 理解图像格式 无论是 HTML 还是 XHTML 都没有规定图像的官方格式 ...
- 自己学习过程中关于以后有可能用到的技术的备份,微信广告滑屏组件 iSlider
转载: iSlider 是个非常平滑的滑块,支持移动端 WebApp,HTML5App 和混合型的 App. iSlider是移动端的滑动组件的最佳解决方案.他和普通的web 端的滑动插件有很大不同, ...
- header("Location:login.php")
header("Location:login.php")应该注意的几个问题 header("Location:")作为php的转向语句.其实在使用中,他有几点 ...
- Activity生命周期回顾
先来一张经典的生命周期图: ------------------------------------------------------------- 工程代码: ActivityLifeCycle. ...
- DATE 使用
DATE 使用 标签(空格分隔): SHELL 使用shell处理文本时经常要使用date,但各种参数经常忘,记录在此: #date 获取当前时间 #date -d "-1 week&quo ...
- PyQt5创建第一个窗体(正规套路)
一.Pyqt5 创建第一个窗体 很多人写窗体程序都是直接敲代码,不使用设计器,我个人不是很赞成这种做法.使用设计器的好处是直观.维护方便,尤其开发复杂窗体的效率高. 但是每次修改ui文件后,需要重新生 ...
- 简单学C——第五天
结构体 首先明确,结构体是一种构造的数据类型,是一种由多个数据类型如 int,char,double,数组或者结构体......组成的类型,现在告诉大家如何定义一个结构体.在定义int整型变量时,大家 ...
- 练习2 G题 - 数值统计
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 统计给 ...