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 git 安装、生成sshkey、克隆、切换分支
#1.安装git apt-get install git; #2生成公钥私钥文件 2.配置git账户: git config --global user.name "yourname&quo ...
- hdu3415 Max Sum of Max-K-sub-sequence 单调队列
//hdu3415 Max Sum of Max-K-sub-sequence //单调队列 //首先想到了预处理出前缀和利用s[i] - s[j]表示(j,i]段的和 //之后的问题就转换成了求一个 ...
- 带圈圈的数字1~50,求50以上,不要word的
①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿
- asp.net 复习总结
1.asp.net页面上格式化时间是:<%# Eval("jsBianhao", "{0:yyyy/MM/dd}")%>
- Android中Scrollview、ViewPager冲突问题汇总(已解决)
显示问题: 1.设置ScrollView的fillViewPort为true 2.设置ViewPager的layout_height为一个固定高度,比如:100dip 冲突问题: 不过ViewPage ...
- html&css基础框架
原文地址:http://www.w3cplus.com/framework/index.php
- 【C#】重载重写重构
前言 前几篇博客说了重写和重载.今天主要说重构,顺便比較一下三者. 重构.重写.重载 重构就是通过调整程序代码改善软件的质量.性能,使其程序的设计模式和架构更趋合理.提高软件的扩展性和维护性. 通俗点 ...
- Android N(7.0) 在ListView里显示EditText时软键盘弹出时会自动切换到全键盘的问题?
Android N(7.0) 在ListView里显示EditText时软键盘弹出时会自动切换到全键盘的问题? 问题症状描述 Activity 在AndroidManifest.xml里设置andro ...
- Atitit.java 反编译 工具 attilax 总结
Atitit.java 反编译 工具 attilax 总结 1. 三大核心核心引擎——1 2. JAD Jad attitude推荐这个1 2.1. Jdec.2 2.2. 二. 源码开放的 J ...
- testng入门_单元测试
1.定义TestNG 的配置文件 <test name="exampletest1"> <classes> <!--1.只执行com.t ...