题意就是给你一张无向连通图,试问对于图上所有点对(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的更多相关文章

  1. cogs—— 310. [POJ2395] Out of Hay

    310. [POJ2395] Out of Hay ★☆   输入文件:outofhay.in   输出文件:outofhay.out   简单对比 时间限制:1 s   内存限制:128 MB De ...

  2. POJ2395 Out of Hay(求最小生成树中最大的边权,Kruskal)

    POJ2395 Out of Hay 寻找最小生成树中最大的边权. 使用 Kruskal 求解,即求选取的第 \(n-1\) 条合法边. 时间复杂度为 \(O(e\log e)\) . #includ ...

  3. Out of Hay(poj2395)(并查集)

    Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11580   Accepted: 4515 Descr ...

  4. 洛谷P1547 Out of Hay

    题目背景 奶牛爱干草 题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,0 ...

  5. 瓶颈生成树与最小生成树 POJ 2395 Out of Hay

    百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...

  6. poj2485&&poj2395 kruskal

    题意:最小生成树的最大边最小,sort从小到大即可 poj2485 #include<stdio.h> #include<string.h> #include<algor ...

  7. 洛谷P2925 [USACO08DEC]干草出售Hay For Sale

    题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...

  8. [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草

    [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草 试题描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草. 他知道N(1≤N≤100)个干草 ...

  9. Out of Hay

    Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13094 Accepted: 5078 Descripti ...

随机推荐

  1. Leetcode: Palindrome Numbers

    Determine whether an integer is a palindrome. Do this without extra space. 尝试用两头分别比较的方法,结果发现无法解决1000 ...

  2. 转:python webdriver API 之对话框处理

    页面上弹出的对话框是自动化测试经常会遇到的一个问题:很多情况下对话框是一个 iframe,如上一节中介绍的例子,处理起来稍微有点麻烦:但现在很多前端框架的对话框是 div 形式的,这就让我们的处理变得 ...

  3. csuoj 1022: 菜鸟和大牛

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1022 1022: 菜鸟和大牛 Time Limit: 1 Sec  Memory Limit: 1 ...

  4. Missing artifact com.sun:tools:jar 1.5.0 终极解决方法

    在使用m2eclipse插件时,在pom.xml中添加struts2-core.jar包后,需要依赖java运行时的tools.jar进行依赖.但是,此时eclipse无法读取tools包,出现如下错 ...

  5. 解析xml文件

    package com.ss1.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOExceptio ...

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON CheckDifference

    zw版[转发·台湾nvp系列Delphi例程]HALCON CheckDifference unit Unit1;interfaceuses Windows, Messages, SysUtils, ...

  7. 将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十)[转]

    前一段时间,讲了一系列开发经理.实施经理.服务经理的工具箱:开发经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三) ,实施经理的工具箱--走出软件作坊:三五个人十来条枪 如何成 ...

  8. 视频处理控件TVideoGrabber部分技术问题解答

    TVideoGrabber是一个功能全面.易于使用的视频捕捉工具和多媒体播放器,本文搜集了一些TVideoGrabber的技术问答,并针对于有的朋友遇到的疑难给出了解答. 一.在TVideoGrabb ...

  9. Linux终端乱码的解决办法

    用SSH连接Linux时经常会遇到乱码的情况,痛苦了好久,在网上找到一个解决办法,编辑~/.bash_profile文件,加入下面两行: LANG="zh_CN.GB18030" ...

  10. Android 常用工具类之LogUtil,可以定位到代码行,双击跳转

    package cn.utils; import android.util.Log; public class LogUtils { public static boolean isDebug = t ...