POJ 2485 Highways【最小生成树最大权——简单模板】
链接:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18668 | Accepted: 8648 |
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
Hint
Source
题意:
通:
Kruskal:
| Accepted | 820K | 235MS | C++ | 1265B |
#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn = 500+10; int w[maxn][maxn];
int p[maxn];
int n,m;
int MaxWeight; struct Edge{
int u,v;
int w;
}edge[maxn*maxn/2]; bool cmp(Edge a, Edge b)
{
return a.w < b.w;
} int find(int x)
{
return x == p[x] ? x : p[x] = find(p[x]);
} void Kruskal()
{
for(int i = 1; i <= n; i++) p[i] = i;
sort(edge,edge+m,cmp); for(int i = 0; i < m; i++)
{
int u = find(edge[i].u);
int v = find(edge[i].v); if(u != v)
{
p[v] = u;
MaxWeight = max(MaxWeight, edge[i].w);
}
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n); for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &w[i][j]); m = 0;
for(int i = 1; i <= n; i++)
{
for(int j = i+1; j <= n; j++)
{
edge[m].u = i;
edge[m].v = j;
edge[m++].w = w[i][j];
}
} MaxWeight = 0;
Kruskal(); printf("%d\n", MaxWeight);
}
return 0;
}
Prime:
| Accepted | 580K | 172MS | C++ | 943B |
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 510;
const int INF = 65536*maxn; int w[maxn][maxn];
int d[maxn];
int vis[maxn];
int n;
int MaxWeight; void Prime()
{
for(int i = 1; i <= n; i++) d[i] = INF;
d[1] = 0;
memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++)
{
int x, m = INF;
for(int y = 1; y <= n; y++) if(!vis[y] && d[y] <= m) m = d[x=y];
vis[x] = 1; MaxWeight = max(MaxWeight, d[x]);
for(int y = 1; y <= n; y++) if(!vis[y])
d[y] = min(d[y], w[x][y]);
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &w[i][j]); MaxWeight = 0;
Prime();
printf("%d\n", MaxWeight);
}
return 0;
}
POJ 2485 Highways【最小生成树最大权——简单模板】的更多相关文章
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- POJ 2485 Highways (求最小生成树中最大的边)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 2485 Highways (prim最小生成树)
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...
- poj 2485 Highways(最小生成树,基础,最大边权)
题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...
随机推荐
- Ubuntu——修正LibreOffice中文乱码以及eclipse提示框颜色
刚进ubuntu的时候就老遇到用libreoffice打开某些文档时出现一堆叠在一起的乱码.以及最開始使用eclipse编辑时,鼠标放到一个变量或者函数时 弹出来一黑框框-看的难受的.... 这里记录 ...
- Srping AOP xml方式
使用aop需要: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- iOS学习笔记-自定义过渡动画
代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...
- 【C语言天天练(二三)】errno变量
引言: 在C编程中,errno是个必不可少的变量.特别是在网络编程中. 假设你没实用过errno,那仅仅能说明你的程序不够健壮. 为什么会使用errno呢?这是系统库设计中的一个无奈之举.他很多其它的 ...
- C#实现svn server端的hook
目标 要做的东东呢,就是在向svn提交文件的时候,可以再server端读到所有提交文件的内容,并根据某些规则验证文件的合法性,如果验证失败,则终止提交,并在svn的客户端上显示错误信息. 准备工作 ...
- svn的外网设置访问方法
一.花生壳 1. 设置静态ip 选择 DHCP服务器 -> 静态地址分配,为内网内的机器分配静态ip 2.设置端口转发 选择 转发规则 -> 虚拟服务器,将外网对443端口的访问转发到安装 ...
- websocket echo test
http://www.websocket.org/echo.html .net websocket server http://superwebsocket.codeplex.com/ http:// ...
- DM36x IPNC OSD显示中文 --- 基本数据准备篇
经过上一篇的叙述,基本原理搞清楚后,便需要对我们在OSD上显示中文作数据准备,首先是需要将gb2312关键区(也就是实际有文字存在的区)中的汉字转换为图片,在实际的转换中,并不像上一篇中GB2312编 ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- vue的计算属性
在模板中写入过多的逻辑使模板过重且难以维护.因此有了计算属性(computed)的产生. 你可以像绑定普通属性一样在模板中绑定计算属性,vue知道计算属性中的函数依赖data中的数据.所以当data中 ...