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 ...
随机推荐
- axios中为所有请求带上Token头
axios中为所有请求带上Token头 https://www.imooc.com/article/27751
- AspNetCore容器化(Docker)部署(一) —— 入门
一.docker注册安装 Windows Docker Desktop https://www.docker.com/products/docker-desktop Linux Docker CE h ...
- 《3+1团队》【Alpha】Scrum meeting 4
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...
- 关于mysql服务突然运行不了的问题-“本地计算机上的mysql服务启动后停止,某些...”
1.将mysql数据库的安装目录bin文件路径添加到环境的path中,为了让cmd可以直接输入下面的相关命令,不然cd到mysql的bin下也可以 2.cmd输入mysqld --initialize ...
- Echarts 异步数据加载遇到的问题
看了Echarts官网异步加载数据的Demo var myChart = echarts.init(document.getElementById('main')); // 显示标题,图例和空的坐标轴 ...
- Linux CentOS 知识和常用命令
1.常用热键 [Tab]它具有“命令补全”与“文件补全”的功能[Ctrl+C]中断执行中的程序组合键[Ctrl+d]键盘输入结束.也可以用来替代 exit 2.Linux 常用编辑器 vi 和 vim ...
- 提高CPU使用率
某些特殊时候,需要提升下cpu的利用率,此时……………………需要一个极其简单的脚本来完成! #!/bin/bash while (true);do { for i in $(seq 100000 10 ...
- 如何用纯 CSS 创作炫酷的同心矩形旋转动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/bMvbRp 可交互视频教 ...
- webservice学习第二天
1 课程回顾 l 什么是webservice 远程调用技术:系统和系统之间的调用,获取远程系统里的业务数据 Webservice使用http传输SOAP协议的数据的一种远程调用技术 l Webserv ...
- PTA 01-复杂度1 最大子列和问题 (20分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/709 5-1 最大子列和问题 (20分) 给定KK个整数组成的序列{ N_1N1 ...