Poj 2395 Out of Hay( 最小生成树 )
题意:求最小生成树中最大的一条边。
分析:求最小生成树,可用Prim和Kruskal算法。一般稀疏图用Kruskal比较适合,稠密图用Prim。由于Kruskal的思想是把非连通的N个顶点用最小的代价构成一个连通分量,这与并查集的思想类似,所以可以用并查集来实现Kruskal。
import java.util.Scanner; /**
* 稀疏图用Prim,21316K,3047MS 不划算、
*/
public class Poj_2395_Prim { static int n,m;
static int[][] map=new int[2010][2010];
static boolean vis[]=new boolean[2010]; public static int prime() {
int i,j,min,flag = 0,max=-1; vis[1]=true;
for(i=2;i<=n;i++){
min=Integer.MAX_VALUE;
flag=0;
for(j=1;j<=n;j++){
if(!vis[j] && map[1][j] < min){
min=map[1][j];
flag=j;
}
}
vis[flag]=true;
max = max < min ? min : max;
for(j=1;j<=n;j++){
if(!vis[j] && map[flag][j] <map[1][j]){
map[1][j]=map[flag][j];
}
}
}
return max;
} public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt(); for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
map[i][j]=Integer.MAX_VALUE;
}
map[i][i]=0;
} for (int i = 1; i <= m; i++) {
int s=sc.nextInt();
int e=sc.nextInt();
int val=sc.nextInt();
if(map[s][e] > val){
map[s][e] = val;
map[e][s]=val;
}
} System.out.println(prime());
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner; class Edge{
int s;
int e;
int val;
public Edge(int s,int e,int val){
this.s=s;
this.e=e;
this.val=val;
}
} class Com implements Comparator<Edge>{
@Override
public int compare(Edge o1, Edge o2) {
// TODO Auto-generated method stub
return o1.val - o2.val;
}
} /**
*Kruskal
*/
public class Poj_2395_kruskal { static int n,m;
static int MAX = 4000000;
static ArrayList<Edge> list = new ArrayList<Edge>();
static int set[] = new int[MAX]; static void init_set(){
for(int i=1;i<=n;i++){
set[i]=i;
}
} static int find(int a){
if(set[a] == a){
return a;
}else{
return set[a]=find(set[a]);
}
} static void unite(int x,int y){ x = find(x);
y = find(y);
if (x == y)
return;
if (y < x)
set[x] = y;
else if (y > x)
set[y] = x;
} static boolean same(int x, int y){
return find(x) == find(y);
} static int kruskal(){
int ans=-1;
init_set();
for(int i=0;i < list.size();i++){
Edge e =list.get(i);
if (!same(e.s, e.e)){
unite(e.s, e.e);
if (ans < e.val)
ans = e.val;
}
}
return ans;
} public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt(); for (int i = 1; i <= m; i++) {
int s=sc.nextInt();
int e=sc.nextInt();
int val=sc.nextInt();
list.add(new Edge(s,e,val));
}
Collections.sort(list,new Com());
System.out.println(kruskal());
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Poj 2395 Out of Hay( 最小生成树 )的更多相关文章
- POJ 2395 Out of Hay(最小生成树中的最大长度)
POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...
- poj 2395 Out of Hay(最小生成树,水)
Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...
- 瓶颈生成树与最小生成树 POJ 2395 Out of Hay
百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...
- POJ 2395 Out of Hay(求最小生成树的最长边+kruskal)
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18472 Accepted: 7318 Descr ...
- POJ 2395 Out of Hay( 最小生成树 )
链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...
- poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...
- POJ 2395 Out of Hay(MST)
[题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...
- POJ 2395 Out of Hay (prim)
题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...
- POJ 2395 Out of Hay 草荒 (MST,Kruscal,最小瓶颈树)
题意:Bessie要从牧场1到达各大牧场去,他从不关心他要走多远,他只关心他的水袋够不够水,他可以在任意牧场补给水,问他走完各大牧场,最多的一次需要多少带多少单位的水? 思路:其实就是要让所带的水尽量 ...
随机推荐
- Docker容器技术-第一个容器
一.第一个容器 1.Docker版本 A.community-edition社区版 Docker CE是免费的Docker产品的新名称,Docker CE包含了完整的Docker平台,非常适合开发人员 ...
- post请求和get请求content_type的种类
get请求的headers中没有content-type这个字段,post 的 content-type 有两种 : application/x-www-form-urlencoded 这种就是一般的 ...
- eclipse maven 项目 maven build 无反应
eclipse maven 项目 使用maven build ,clean 等命令均无反应,控制台无任何输出 1.打开Window --> Preferences --> Java --& ...
- 39条常见的linux系统管理面试题
1.如何看当前Linux系统有几颗物理CPU和每颗CPU的核数? 答:[root@centos6 ~ 10:55 #35]# cat /proc/cpuinfo|grep -c 'physical i ...
- mysql里的ibdata1文件
mysql大多数磁盘空间被 InnoDB 的共享表空间 ibdata1 使用.而你已经启用了 innodb_file_per_table,所以问题是: ibdata1存了什么? 当你启用了innodb ...
- spark学习2(hive0.13安装)
第一步:hive安装 通过WinSCP将apache-hive-0.13.1-bin.tar.gz上传到/usr/hive/目录下 [root@spark1 hive]# chmod u+x apac ...
- MySql基础学习-总纲
- Spring Boot入门——freemarker
使用步骤: 1.在pom.xml中添加相关依赖 <!-- 添加freemarker依赖 --> <dependency> <groupId>org.springfr ...
- strip_tags--php
函数剥去字符串中的 HTML.XML 以及 PHP 的标签 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. allow 可选.规定允许的标签.这 ...
- Node的异步I/O
node是单线程非阻塞异步I/O的模式. 阻塞I/O:完成整个数据获取的过程: 非阻塞I/O:不带数据,直接立即返回,要获取数据,还需通过文件描述符再次读取. node完成整个异步I/O的有事件循环. ...