zoukankan      html  css  js  c++  java
  • #luogu整理 P1396 营救:生成树算法

    并查集#2

    luogu P1396 营救 :生成树

    题面描述

    给定n个点和m条边,每条边有一定的权值,求找出一条路径,使得这条路径上的每一条边的权值的最大值最小。

    思路

    生成树算法

    把所有边的边权从小到大排序,一点一点地加到图里面,直到图成为一个连通图,实际上就是最小生成树操作。

    代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    int m,n,s,t;
    struct EDGE{
        int x,y,z,nxt;
    }edge[100000];
    int fa[1000000];
    int getfa(int x){
        return fa[x] == x ? x : getfa(fa[x]);
    }
    bool cmp(EDGE a,EDGE b){
        return a.z < b.z;
    }
    int ans ;
    int main(){
        cin >> n >> m >> s >> t;
        for(int i = 1;i <= m; i++){
            cin >> edge[i].x >> edge[i].y >> edge[i].z;
        }
        sort(edge+1,edge + 1 + m, cmp);
        for(int i = 1;i <= n; i++)fa[i] = i;
        for(int i = 1;i <= m; i++){
            int x = getfa(edge[i].x),y = getfa(edge[i].y);
            ans = max(ans,edge[i].z);
            if(x != y){
                fa[x] = fa[y];
                n--;
            }
            if(getfa(s) == getfa(t)){
                cout << ans << endl;
                return 0;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    汇编学习笔记
    英语口语学习
    三层架构与MVC的区别
    “Razor” – a new view engine for ASP.NET
    代码生成
    最热门的敏捷书籍[转]
    推荐工具
    在IBM发现管理的真相[转]
    开源的通用帮助库
    单元测试
  • 原文地址:https://www.cnblogs.com/Cao-Yucong/p/12712555.html
Copyright © 2011-2022 走看看