Highways POJ 2485【Prim】
Description
to drive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town
that is located at the end of both highways.
The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Input
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between
village i and village j. There is an empty line after each test case.
Output
Sample Input
1 3
0 990 692
990 0 179
692 179 0
Sample Output
692
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define N 550
int n;
int i,j;
int map[N][N];
int a,b;
int low[N];
bool vis[N];
int sum[N];
void input()
{
//memset(map,INF,sizeof(map));
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
scanf("%d",map[i]+j);
}
}
}
void prim()
{
int pos=1;
for(i=1;i<=n;++i)//第一次给low赋值
{
low[i]=map[pos][i];
}
vis[pos]=1; //增加最小生成树集合
for(i=1;i<n;++i)//再找n-1个点
{
int min=INF;
for(j=1;j<=n;++j)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
pos=j;//把找到的点记录下
}
}
sum[i]=min;
vis[pos]=1;
for(j=1;j<=n;++j)
{
if(!vis[j]&&low[j]>map[pos][j])
{
low[j]=map[pos][j];
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(vis,0,sizeof(vis));
input();
prim();
int max=-INF;
for(i=1;i<n;++i)
if(max<sum[i])
max=sum[i];
printf("%d\n",max);
}
return 0;
}
Highways POJ 2485【Prim】的更多相关文章
- poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】
含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解. Desert King Time Limit: 3000MS Memory Li ...
- 【简●解】POJ 1845 【Sumdiv】
POJ 1845 [Sumdiv] [题目大意] 给定\(A\)和\(B\),求\(A^B\)的所有约数之和,对\(9901\)取模. (对于全部数据,\(0<= A <= B <= ...
- Highways - poj 2485 (Prim 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24383 Accepted: 11243 Description T ...
- 洛谷1546 最短网络Agri-Net【最小生成树】【prim】
[内含最小生成树Prim模板] 题目:https://www.luogu.org/problemnew/show/P1546 题意:给定一个邻接矩阵.求最小生成树. 思路:点少边多用Prim. Pri ...
- POJ 2154 【POLYA】【欧拉】
前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...
- Highways poj 2485
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 3230 【DP】
题意: 某货旅行,在n个城市呆m天. 给出从第i个城市到第j个城市的路费,或者留在某个城市的生活费. 给出在第i天在第j个城市的收益. 可以在城市之间任意穿梭逗留没有其他特殊要求. 求收益最大是多少. ...
- H - Highways - poj 1751(prim)
某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就 ...
- POJ 3187【permutation】
POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include &l ...
随机推荐
- docker 镜像管理操作
镜像特点 1. 分层存储的文件 2.一个软件运行环境 3.一个镜像可以创建多个容器 4.一种标准交付 5.不包含Linux内核而又精简的Linux操作系统 6.不是一个单一的文件而是由多层构成的,可以 ...
- forward reference extends over definition of value
在scala代码中定义了一个方法,,刚开始直接代码中报错,,后来编译是一直报错,最后只是在sc.stop后边加了一个中括号解决,方法体不能放在main主函数中
- 最短路 || POJ 1511 Invitation Cards
已知图中从一点到另一点的距离,从1号点到另一点再从这一点返回1号点,求去到所有点的距离之和最小值 *解法:正着反着分别建图,把到每个点的距离加起来 spfa跑完之后dist数组就是从起点到每一点的最短 ...
- js模块化入门与commonjs解析与应用
JS模块化的基本原理 commonjs规范 commonjs在前端模块化中的基本使用 AMD与CMD规范剖析博客链接 一.JS模块化基本原理 在JS没有提出来模块化的时候,开发JS项目比较简单,同时也 ...
- 洛谷p1049 01背包
dp水之旅背包 题目描述 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30,每个物品有一个体积(正整数). 要求n个物品中,任取若干个装入箱 ...
- Centos 7 编译nginx 1.14.0
步骤一:下载nginx安装包 wget https://nginx.org/download/nginx-1.14.0.tar.gz 步骤二:安装nginx依赖包 yum install -y gcc ...
- cmd启动MySQL服务器发生错误
Mysql net start mysql启动,提示发生系统错误 5 拒绝访问 原文:https://blog.csdn.net/angel_guoo/article/details/7919037 ...
- Linux基础学习-通过VM安装RHEL7.4
虚拟机安装RHEL7.4 1.VM虚拟机设置 这里我们配置的虚拟机为1核1G,50G硬盘,NAT模式 2.Linux安装 这里时区我们选择中国上海,时间需要调整一下相差8小时. 这里添加一下中文语言支 ...
- Python中的函数(4)
一.传递列表 你经常会发现,向函数传递列表很有用,这种列表包含的可能是名字.数字或者更复杂的对象(如字典). 将列表传递给函数后,函数就能直接访问其内容. 栗子:假设有一个用户列表,我们要和其中每一位 ...
- 【HDU 6006】Engineer Assignment(状压DP)
Problem Description In Google, there are many experts of different areas. For example, MapReduce exp ...