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 ...
随机推荐
- lodash 类型判断
1.isArray _.isArray(value) 检查 value 是否是 Array 类对象. 2.isElement _.isElement(value) 检查 value 是否是可能是 DO ...
- BZOJ 1016 JSOI 2008 最小生成树计数 Kruskal+搜索
题目大意:给出一些边,求出一共能形成多少个最小生成树. 思路:最小生成树有非常多定理啊,我也不是非常明确.这里仅仅简单讲讲做法.关于定各种定理请看这里:http://blog.csdn.net/wyf ...
- OpenGL基础图形编程(八)变换
八.OpenGL变换 OpenGL变换是本篇的重点内容,它包含计算机图形学中最主要的三维变换,即几何变换.投影变换.裁剪变换.视口变换,以及针对OpenGL的特殊变换概念理解和使用方法,如相机模拟.矩 ...
- Spring 读取配置文件(二)
Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...
- cf #363 b
B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Java并发编程(七)线程封闭
当访问共享的可变数据时,通常需要使用同步.一种避免使用同步的方式就是不共享数据. 如果仅在单线程内访问数据,就不需要同步.这种技术被称为线程封闭(Thread Confinement),它是实现线程安 ...
- CPU亲和力
http://blog.chinaunix.net/uid-27714502-id-3515874.html http://www.tuicool.com/articles/I7NFzy http:/ ...
- IRQ与FIQ的区别
1.对FIQ你必须进快处理中断请求,并离开这个模式. 2.IRQ可以被FIQ所中断,但FIQ不能被IRQ所中断,在处理FIQ时必须要关闭中断. 3.FIQ的优先级比IRQ高. 4.FIQ模式下,比IR ...
- windows下mysql区分大小写敏感问题
默认情况下,表别名在Unix上区分大小写,但在Windows或macOS上不是这样.以下语句在Unix上不起作用,因为它引用别名as a和as A: mysql> SELECT col_name ...
- 第一百八十七节,jQuery,知问前端--cookie 插件,注册成功后生成cookie,显示登录状态
jQuery,知问前端--cookie 插件 学习要点: 1.使用 cookie 插件 2.注册直接登录 Cookie 是网站用来在客户端保存识别用户的一种小文件.一般来用库可以保存用户登 录信息.购 ...