poj 2485 Highways (最小生成树)
题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度
这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值。仅仅须要加个推断
比較最小生成树每条边的大小即可
kruskal算法
#include<cstdio>
#include<algorithm>
using namespace std;
int f[510],n,m;
struct stu
{
int a,b,c;
}t[20100];
int cmp(struct stu x,struct stu y)
{
return x.c<y.c;
}
int find(int x)
{
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
int krus()
{
int i,k=0,s=0,x,y;
for(i=1;i<m;i++){
x=find(t[i].a);
y=find(t[i].b);
if(x!=y){
if(t[i].c>s) //比較最小生成树中权值的大小
s=t[i].c;
k++;
if(k==n-1)
break;
f[x]=y;
}
}
return s;
}
int main()
{
int T,i,j,c,s=0;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
m=1;
for(i=1;i<=n;i++){
f[i]=i;
for(j=1;j<=n;j++){
scanf("%d",&c);
if(j<i){
t[m].a=i;
t[m].b=j;
t[m++].c=c;
}
}
}
sort(t+1,t+m,cmp);
s=krus();
printf("%d\n",s);
}
return 0;
}
prim算法
#include<stdio.h>
#include<string.h>
int n,map[505][505],vis[505],low[505];
int prim()
{
int i,j,min,pos,k=-1;
memset(vis,0,sizeof(vis));
pos=1;
vis[pos]=1;
for(i=1;i<=n;i++)
low[i]=map[pos][i];
for(i=1;i<n;i++){
min=99999;
for(j=1;j<=n;j++)
if(!vis[j]&&low[j]<min){
min=low[j];
pos=j;
}
if(min>k)
k=min;
vis[pos]=1;
for(j=1;j<=n;j++)
if(!vis[j]&&map[pos][j]<low[j])
low[j]=map[pos][j];
}
return k;
}
int main()
{
int T,i,j,s;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
s=prim();
printf("%d\n",s);
}
return 0;
}
poj 2485 Highways (最小生成树)的更多相关文章
- 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 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- 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 ...
随机推荐
- JS中的兼容问题总结
今天总结总结在JS里面遇到的兼容性问题 1.获取滚动距离的兼容性问题: document.documentElement.scrollTop || document.body.scrollTop ...
- 从零开始学习html(十三) CSS代码缩写,占用更少的带宽
一.盒模型代码简写 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type&quo ...
- npm、cnpm、bower安装区别
简单地说,就是帮你下载好你需要的css或者js库,而且三者功能也都是一样的.那为什么要下载这3个不同的呢?据说npm容易被墙……而cnpm是淘宝的镜像,所以通常用cnpm代替npm.至于bower,是 ...
- html垂直居中
参考于http://www.cnblogs.com/yugege/p/5246652.html <!DOCTYPE html> <html lang="en"&g ...
- Android Application中的Context和Activity中的Context的异同
一.Context是什么: 1.Context是维持Android程序中各组件能够正常工作的一个核心功能类,我们选中Context类 ,按下快捷键F4,右边就会出现一个Context类的继承结构图啦, ...
- 机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归
机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归 关键字:Logistic回归.python.源码解析.测试作者:米仓山下时间:2018- ...
- AOP编程报错Xlint:invalidAbsoluteTypeName
@Component@Aspectpublic class DingdingAspect { private Logger logger = LoggerFactory.getLogger(this. ...
- 单元测试,模拟用户Get登陆,并携带登录后的token访问接口
HttpClient _httpClient; HttpClient _businessHttpClient; private async Task<string> GetAccessTo ...
- 《MVC+EF》——用DBFirst创建ADO.NET实体数据模型和对象关系映射
转载于:http://blog.csdn.net/zhoukun1008/article/details/50528145 现在越来越喜欢MVC了,不光是因为ITOO中用到了他,而是因为它里面包含了很 ...
- Oracle EBS AR 客户API
------------------------------------ 1. Set Environment ------------------------------------ -- 1a. ...