最小生成树指的是在图上面找到权值最小的一棵树,并且保证图上所有的点都在这棵树上。

解决办法:Kruskal 算法(贪心思想)

将边按权值从小到大排序,然后按这个顺序不断连边,直到所有点联通。

/**
 *  Fuck you.
 *  I love you too.
 */

#include<bits/stdc++.h>
#define lson i<<2
#define rson i<<2|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define mem(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&-x)
#define enld endl
#define mian main
#define itn int
#define prinft printf

const double PI = acos (-1.0);
const int INF = 0x3f3f3f3f;
const int EXP = 1e-8;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;

using namespace std;

int n;
int ans;
int par[N];

struct ed {
    int s, e, cost;
} edge[MAXN];

bool cmp (ed a, ed b) {
    return a.cost < b.cost;
}

int Find (int a) {
    return a == par[a] ? a : par[a] = Find (par[a]);
}

void join (int a, int b) {
    par[Find (a)] = Find (b);
}

int main() {
    while (~scanf ("%d", &n)) {
        ans = 0;
        for (int i = 0; i < n; ++i) {
            scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost);
            par[edge[i].s] = edge[i].s;
            par[edge[i].e] = edge[i].e;
        }
        sort (edge, edge + n, cmp);
        for (int i = 0; i < n; ++i) {
            if (Find (edge[i].s) != Find (edge[i].e)) {
                cout << edge[i].s << ' ' << edge[i].e << endl;
                join (edge[i].s, edge[i].e);
                ans += edge[i].cost;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

  

Hdu - 1162 Eddy's picture (完全图)

 /**
  *  Fuck you.
  *  I love you too.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 int n;
 int ans;
 int par[N];

 struct ed {
     int s, e, cost;
 } edge[MAXN];

 bool cmp (ed a, ed b) {
     return a.cost < b.cost;
 }

 int Find (int a) {
     return a == par[a] ? a : par[a] = Find (par[a]);
 }

 void join (int a, int b) {
     par[Find (a)] = Find (b);
 }

 int main() {
     while (~scanf ("%d", &n)) {
         ans = ;
         ; i < n; ++i) {
             scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost);
             par[edge[i].s] = edge[i].s;
             par[edge[i].e] = edge[i].e;
         }
         sort (edge, edge + n, cmp);
         ; i < n; ++i) {
             if (Find (edge[i].s) != Find (edge[i].e)) {
                 cout << edge[i].s << ' ' << edge[i].e << endl;
                 join (edge[i].s, edge[i].e);
                 ans += edge[i].cost;
             }
         }
         cout << ans << endl;
     }
     ;
 }

Hdu - 1233 还是畅通工程(将上面的模板添加一行即可AC)

 /**
  *  Fuck you.
  *  I love you too.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 int n;
 int ans;
 int par[N];

 struct ed {
     int s, e, cost;
 } edge[MAXN];

 bool cmp (ed a, ed b) {
     return a.cost < b.cost;
 }

 int Find (int a) {
     return a == par[a] ? a : par[a] = Find (par[a]);
 }

 void join (int a, int b) {
     par[Find (a)] = Find (b);
 }

 int main() {
     ) {
         ans = ;
         n = n * (n - ) / ;
         ; i < n; ++i) {
             scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost);
             par[edge[i].s] = edge[i].s;
             par[edge[i].e] = edge[i].e;
         }
         sort (edge, edge + n, cmp);
         ; i < n; ++i) {
             if (Find (edge[i].s) != Find (edge[i].e)) {
                 //cout << edge[i].s << ' ' << edge[i].e << endl;
                 join (edge[i].s, edge[i].e);
                 ans += edge[i].cost;
             }
         }
         cout << ans << endl;
     }
     ;
 }

Hdu - 1598 find the most comfortable road

并查集 + 枚举

先排序(从大到小,从小到大都行),再枚举所有的边,记录其中的最大最小值,更新答案。

 /**
  *  Fuck you.
  *  I love you too.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 int n, m, q;
 int Max, Min, ans;
 int x, y;
 int par[N];

 struct ed {
     int s, e, cost;
 } edge[MAXN];

 bool cmp (ed a, ed b) {
     return a.cost < b.cost;
 }

 int Find (int a) {
     return a == par[a] ? a : par[a] = Find (par[a]);
 }

 void join (int a, int b) {
     par[Find (a)] = Find (b);
 }

 void init() {
     ; i < m+; ++i)
         par[i] = i;
 }

 int main() {
     while (~scanf ("%d%d", &n, &m)) {
         mem (par, );
         ; i < m; i++) {
             scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost);
         }
         sort (edge, edge + m, cmp);
         scanf ("%d", &q);
         while (q--) {
             scanf ("%d%d", &x, &y);
             ans = INF;
             ; j < m; j++) {
                 init();
                 Min = INF, Max = ;
                 for (int i = j; i < m; i++) {
                     if (Find (edge[i].s) != Find (edge[i].e)) {
                         join (edge[i].s, edge[i].e);
                         Min = min (Min, edge[i].cost), Max = max (Max, edge[i].cost);
                     }
                     if (Find (x) == Find (y)) {
                         ans = min (ans, Max - Min);
                         break;
                     }
                 }
             }
             if (ans == INF)
                 cout << "-1" << endl;
             else {
                 cout << ans << endl;
             }
         }
     }
     ;
 }

Kruskal 模板的更多相关文章

  1. Kruskal模板

    Kruskal模板 struct Edge { int from,to,v; }edge[maxn*10]; int fa[maxn]; int n,m; int find(int x) { retu ...

  2. POJ 1287 Networking【kruskal模板题】

    传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...

  3. 最小生成树kruskal模板

    算法思路:每次选取权值最小的边,判断这两个点是否在同一个集合内,如果在则跳过,如果不在则加上这条边的权值 可以使用并查集储存结点,可以快速判断结点是否在同一集合内. #include<iostr ...

  4. 最小生成树prim和kruskal模板

    prim: int cost[MAX_V][MAX_V]; //cost[u][v]表示边e=(u,v)的权值(不存在的情况下设为INF) int mincost[MAX_V]; //从集合X出发的每 ...

  5. HDU 2988 Dark roads(kruskal模板题)

    Dark roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. 【还是畅通工程 HDU - 1233】【Kruskal模板题】

    Kruskal算法讲解 该部分内容全部摘录自刘汝佳的<算法竞赛入门经典> Kruskal算法的第一步是给所有边按照从小到大的顺序排列. 这一步可以直接使用库函数 qsort或者sort. ...

  7. Prime算法 与 Kruskal算法求最小生成树模板

    算法原理参考链接 ==> UESTC算法讲堂——最小生成树 关于两种算法的复杂度分析 ==> http://blog.csdn.net/haskei/article/details/531 ...

  8. 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法

    Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...

  9. 【次小生成树】【Kruskal】【prim】【转】

    原博客出处:https://blog.csdn.net/yasola/article/details/74276255 通常次小生成树是使用Prim算法进行实现的,因为可以在Prim算法松弛的同时求得 ...

随机推荐

  1. 流媒体技术学习笔记之(十二)Linux(Ubuntu)环境运行EasyDarwin

    Debug问题??? ./easydarwin -c ./easydarwin.xml & //这样的话是80端口 ./easydarwin -c ./easydarwin.xml -d // ...

  2. python 通用字典方法

    版本1 方法 # 不传返回所有属性,传入props只返回传入的对应属性 def m_dict(obj, props=[]): result = {} target = obj else props f ...

  3. ASP.NET Web API queryString访问的一点总结

    自从开始使用ASP.NET Web API,各种路由的蛋疼问题一直困扰着我,相信大家也都一样. Web API的路由配置与ASP.MVC类似,在App_Start文件夹下,有一个WebApiConfi ...

  4. 【转】UICollectionView使用介绍

    CHENYILONG Blog UICollectionView 使用介绍 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/lu ...

  5. hadoop - hdfs 基础操作

    hdfs --help # 所有参数 hdfs dfs -help # 运行文件系统命令在Hadoop文件系统 hdfs dfs -ls /logs # 查看 hdfs dfs -ls /user/ ...

  6. charles https抓包 (安卓安装证书)

    的Android APP使用的都是http请求,之后改成了https,就出现了以下情况,无法正常读取抓取的内容 下面阐述一下,正确的安装步骤,为出现类似情况的朋友提供一个参考: 1.第一步: 最后点击 ...

  7. 如何得到Slave应用relay-log的时间

    官方社区版MySQL 5.7.19 基于Row+Position搭建的一主一从异步复制结构:Master->{Slave} ROLE HOSTNAME BASEDIR DATADIR IP PO ...

  8. nested exception is com.svorx.core.dao.PersistenceException

    在quartz定时执行任务的时候,hibernate报错,在只读事务中进行了update语句: [ERROR] 2018/08/03 10:35:00,827 org.quartz.core.JobR ...

  9. Oracle 11g R2 32位 & Oracle 11g R2 64位 -百度云下载

    Oracle 11g R2 32位 & Oracle 11g R2 64位 -百度云下载 https://pan.baidu.com/s/1fuzy67Olfxzsy3WJMCrCnQ 提取码 ...

  10. 加载JS代码

    玩转JS系列之代码加载篇   一开始我们这样写js <script type="text/javascript"> function a(){ console.log( ...