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 ...
随机推荐
- Wcf Restful Service服务搭建
目的 使用Wcf(C#)搭建一个Restful Service 背景 最近接到一个项目,客户要求使用Restful 方式接收到数据,并对数据提供对数据的统计显示功能,简单是简单,但必须要使用Restf ...
- GTA项目 三, 使用 bootstrap table展示界面,使得data和UI分离
/** bootstrap-table - v1.5.0 - 2014-12-12* https://github.com/wenzhixin/bootstrap-table* Copyright ( ...
- Swift实战-豆瓣电台(四)歌曲列表的展现
观看地址 http://v.youku.com/v_show/id_XNzMwNDE0OTA4.html 这节的主要内容是如何利用cell展现获取到的数据. 首先申明两个数组来储存我们获取到的数据 v ...
- 很赞的PHP字符串加密函数
最近, 从discuz里面发现了一个很牛的加密解密函数. 此函数的厉害之处在于可以在指定时间内加密还原字符串,超时无法还原 这样我们就可以拿此函数来做很多用途了,比如:单点登录的token加密传输啦, ...
- C++Builder 解决绘图闪动问题
使用双缓冲 Form->DoubleBuffered = true; Panel->DoubleBuffered = true;
- codeforces-Glass Carving(527C)std::set用法
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- acm算法模板(5)
STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...
- C++之路起航——标准模板库(queue)
queue: FIFO队列:先进先出队列. 优先队列:对队列中的元素按优先级的大小输出. 定义: FIFO队列: queue<数据类性>变量名. 优先队列:priority_queue&l ...
- Git 代码管理常用命令
1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git查看远程仓库:$ git remote -v添加远程仓库:$ git re ...
- JS_01_入门学习
01:编写js文件:JavaScript_01.js // JavaScript Document //1.利用JavaScript来输出文字 <script type="text/j ...