poj2395

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
struct edge
{
    int u,v,dis;
}es[540000];
bool cmp(const edge &e1,const edge &e2){return e1.dis<e2.dis;}
int n,m;
int fa[100010];
void initu(int n,int m)
{
    for(int i=0;i<m;i++)scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].dis);
    for(int i=0;i<n;i++)fa[i]=i;
}
int find(int x){return x==fa[x] ? x : fa[x]=find(fa[x]);}
bool same(int x,int y){return find(x)==find(y);}
void unite(int x,int y){fa[x]=y;}
int kruskal()
{
    int maxn=-1;
    for(int i=0;i<m;i++)
    {
        edge e=es[i];
        if(!same(e.u,e.v))
        {
            maxn=max(maxn,e.dis);
            unite(find(e.u),find(e.v));
        }
    }
    return maxn;
}
int main()
{

    scanf("%d%d",&n,&m);
    initu(n,m);
    sort(es,es+m,cmp);
    int ans=kruskal();
    printf("%d",ans);
    //system("pause");
    return 0;
}

求图中最小生成树中的最长边

kruskal

千万别信《挑战程序设计》里的kruskal

真他妈坑

时间: 2024-11-05 13:48:28

poj2395的相关文章

poj2395 Out of Hay , 求MST的最长边

点击打开链接 求MST的最长边~ prim #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #define Min(a,b) (a)<(b)?(a):(b) using namespace std; const int INF = 1000000000; const int maxn = 2000 + 5; struct pto { int v, l

poj2485&amp;&amp;poj2395 kruskal

题意:最小生成树的最大边最小,sort从小到大即可 poj2485 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define maxn 505 int map[maxn][maxn],pa[150000],num,n; struct node { int x; int y; int val; }s[150000]; bool cmp(node a,node

POJ2395 Out of Hay【Kruskal】

Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11656Accepted: 4562 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situat

Out of Hay(poj2395)(并查集)

Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11580   Accepted: 4515 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay s

poj2395 Out of Hay

题意就是给你一张无向连通图,试问对于图上所有点对(u,v)从u到v的所有路径中边权最大值的最小值的最大值. 定义f(u,v)表示从u到v所有路径中边权最大值的最小值,对所有点对取其最大. 实际上就是求图G的最小生成树的最大边权. 考虑kruskal算法流程,每次选取边权最小的且不产生圈的边加入mst. 至算法结束,图恰好连通,并且选取的边权都是最小的. 对于那些产生回路的边加入到mst中是没有意义的,因为之前保持图连通时选取的边权更小. 注意考虑重边. http://poj.org/proble

POJ2395 Out of Hay 【Dijkstra】

Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11610   Accepted: 4535 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay s