poj2395 Out of Hay
题意就是给你一张无向连通图,试问对于图上所有点对(u,v)从u到v的所有路径中边权最大值的最小值的最大值。
定义f(u,v)表示从u到v所有路径中边权最大值的最小值,对所有点对取其最大。
实际上就是求图G的最小生成树的最大边权。
考虑kruskal算法流程,每次选取边权最小的且不产生圈的边加入mst。
至算法结束,图恰好连通,并且选取的边权都是最小的。
对于那些产生回路的边加入到mst中是没有意义的,因为之前保持图连通时选取的边权更小。
注意考虑重边。
http://poj.org/problem?id=2395
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
typedef __int64 LL;
const int maxn = 2e3 + ;
const int maxm = 1e4 + ;
const int inf = 1e9 + 1e8;
struct Edge{
int from, to, next, c;
bool operator < (const Edge& rhs) const{
return c < rhs.c;
}
}edge[maxm << ];
struct Point{
int x, y, c;
bool operator < (const Point& rhs) const{
return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && c < rhs.c);
}
};
int head[maxn], N;
Point a[maxm];
int n, m, k;
int ans;
bool vis[maxn]; void addEdge(int u, int v, int c){
edge[N].next = head[u];
edge[N].from = u;
edge[N].to = v;
edge[N].c = c;
head[u] = N++;
} int fa[maxn]; int find(int u){
if(fa[u] == - || fa[u] == u) return u;
return fa[u] = find(fa[u]);
}
int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d", &n, &m)){
N = ;
memset(head, -, sizeof head);
for(int i = ; i < m; i++){
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].c);
if(a[i].x > a[i].y) swap(a[i].x, a[i].y);
}
sort(a, a + m);
k = ;
for(int i = ; i < m; i++){
a[k++] = a[i];
while(i < m - && a[i].x == a[i + ].x && a[i].y == a[i + ].y) ++i;
}
for(int i = ; i < k; i++) addEdge(a[i].x, a[i].y, a[i].c);
sort(edge, edge + N);
int ans = -;
memset(fa, -, sizeof fa);
for(int i = ; i < N; i++){
int u = edge[i].from, v = edge[i].to;
int fu = find(u), fv = find(v);
if(fu != fv) fa[fv] = fu, ans = max(ans, edge[i].c);
}
printf("%d\n", ans);
}
return ;
}
poj2395 Out of Hay的更多相关文章
- cogs—— 310. [POJ2395] Out of Hay
310. [POJ2395] Out of Hay ★☆ 输入文件:outofhay.in 输出文件:outofhay.out 简单对比 时间限制:1 s 内存限制:128 MB De ...
- POJ2395 Out of Hay(求最小生成树中最大的边权,Kruskal)
POJ2395 Out of Hay 寻找最小生成树中最大的边权. 使用 Kruskal 求解,即求选取的第 \(n-1\) 条合法边. 时间复杂度为 \(O(e\log e)\) . #includ ...
- Out of Hay(poj2395)(并查集)
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11580 Accepted: 4515 Descr ...
- 洛谷P1547 Out of Hay
题目背景 奶牛爱干草 题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,0 ...
- 瓶颈生成树与最小生成树 POJ 2395 Out of Hay
百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...
- poj2485&&poj2395 kruskal
题意:最小生成树的最大边最小,sort从小到大即可 poj2485 #include<stdio.h> #include<string.h> #include<algor ...
- 洛谷P2925 [USACO08DEC]干草出售Hay For Sale
题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...
- [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草
[BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草 试题描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草. 他知道N(1≤N≤100)个干草 ...
- Out of Hay
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13094 Accepted: 5078 Descripti ...
随机推荐
- Java基础之写文件——使用Formatter对象加载缓冲区(UsingAFormatter)
控制台程序,使用Formatter对象将写入文件的数据准备好. 使用Formatter对象的format()方法,将数据值格式化到视图缓冲区charBuf中. import static java.n ...
- JAX-WS(三)构建简单webservice部署到tomcat上
前言: 虽然构建本地的jax-ws的webservice很简单,但要部署到tomcat上要绕过点弯. tomcat本身和jdk都没有jaw-ws的API,所以部署的时候需要额外做点事情,有两种选择 1 ...
- Ways to access Oracle Database in PostgreSQL
Today, organizations stores information(data) in different database systems. Each database system ha ...
- Codeforce Round #227 Div2
这回的看错时间了! 发现理论可以涨分的- -
- [原创]java WEB学习笔记78:Hibernate学习之路---session概述,session缓存(hibernate 一级缓存),数据库的隔离级别,在 MySql 中设置隔离级别,在 Hibernate 中设置隔离级别
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Python和Ruby抓取网页时的中文乱码问题(在Eclipse和Apatana Studio下均是这种解决方法
Python抓取中文网页乱码 :Eclipse+pydev2.2+python2.7 :Apatana Studio3+ pydev2.2+python2.7 run时设置 run--&g ...
- Eclipse安装Ruby插件应该注意的几点
http://www.aptana.com/products/studio3/success_plugin.html Installing via Eclipse Please copy the fo ...
- JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面
将Map集合对象转换为JSon格式的对象字符串,返回给界面 需导入的jar包: 编写servlet: package com.west.webcourse.servlet; import java.i ...
- logstash5.x改变
5.x版本 logstash中 elasticsearch插件的workers,无法配置大于1,会提示 This plugin uses the shared and doesn't need thi ...
- Mysql自定义函数总结
存储函数 创建存储函数,需要使用CREATE FUNCTION语句,基本语法如下: CREATE FUNCTION func_name([func_parameter]) RETURNS TYPE [ ...